<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
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |