Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone.js"></script>
<script src="https://cdn.rawgit.com/marionettejs/backbone.marionette/master/lib/backbone.marionette.js"></script>
<script src="https://cdn.rawgit.com/Squareknot/marionette.state/v1.0.1/build/marionette.state.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    label {
      display: block;
    }
    .app {}
    .user {
      margin-top: 20px;
    }
    .edit-user {
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <main>
    <div id="app-info-region"></div>
    <div id="open-user-region"></div>
    <div id="user-profile-region"></div>
  </main>
  <script type="text/template" id="app-tpl">
    <h2>App Info</h2>
    Logged-in User ID: <%- loggedInUserId %>
  </script>
  <script type="text/template" id="edit-user-tpl">
    <h2>Open User Profile</h2>
    <label>
      User ID
      <input type="text" name="user" />
    </label>
    <label>
      <button type="button" name="changeUser">View Profile</button>
    </label>
  </script>
  <script type="text/template" id="user-tpl">
    <h2>Profile for User <%- userId %></h2>
    <div>
      Is Logged-in User? <%- isLoggedInUser %>
    </div>  
  </script>
</body>
</html>
 
var AppView = Mn.ItemView.extend({
  template: _.template($('#app-tpl').html()),
  className: 'app',
  modelEvents: {
    'change': 'render'
  }
});
var OpenUserView = Mn.ItemView.extend({
  template: _.template($('#edit-user-tpl').html()),
  className: 'edit-user',
  tagName: 'form',
  initialize: function(options) {
    this.userModel = options.userModel;
  },
  ui: {
    userInput: 'input',
    changeUserButton: 'button'
  },
  events: {
    'click @ui.changeUserButton': 'onClickChangeUserButton'
  },
  onClickChangeUserButton: function() {
    this.userModel.set('id', parseInt(this.ui.userInput.val(), 10));
  }
});
var UserProfileState = Mn.State.extend({
  defaultState: {
    userId: '',
    isLoggedInUser: false
  },
  
  userModelEvents: {
    'change': 'onUserModelChange'
  },
  
  appModelEvents: {
    'change': 'onAppModelChange'
  },
  
  initialize: function(options) {
    this.userModel = options.userModel;
    this.appModel = options.appModel;
    this.syncEntityEvents(this.userModel, this.userModelEvents);
    this.syncEntityEvents(this.appModel, this.appModelEvents);
  },
  
  onUserModelChange: function(userModel) {
    this.set({
      userId: userModel.id,
      isLoggedInUser: userModel.id === this.appModel.get('loggedInUserId')
    });
  },
  
  onAppModelChange: function(appModel) {
    this.set({
      isLoggedInUser: appModel.get('loggedInUserId') === this.userModel.id
    });
  }
});
var UserProfileView = Mn.ItemView.extend({
  template: _.template($('#user-tpl').html()),
  className: 'user',
  constructor: function(options) {
    this.model = new UserProfileState({
      userModel: options.userModel,
      appModel: options.appModel,
      component: this
    });
    Mn.ItemView.prototype.constructor.call(this, options);
  },
  modelEvents: {
    'change': 'render'
  }
});
var UserModel = Backbone.Model.extend({
  defaults: {
    id: 1
  }
});
var AppModel = Backbone.Model.extend({
   defaults: {
     loggedInUserId: 1
   }
});
var userModel = new UserModel();
var appModel = new AppModel();
var appRegion = new Mn.Region({
  el: '#app-info-region'
});
var openUserRegion = new Mn.Region({
  el: '#open-user-region'
});
var userRegion = new Mn.Region({
  el: '#user-profile-region'
});
appRegion.show(new AppView({
  model: appModel
}));
openUserRegion.show(new OpenUserView({
   userModel: userModel
}));
userRegion.show(new UserProfileView({
  userModel: userModel,
  appModel: appModel
}));
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers