Skip to content

Instantly share code, notes, and snippets.

@tonylukasavage
Forked from timanrebel/Profile.js
Last active January 4, 2016 00:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonylukasavage/8541502 to your computer and use it in GitHub Desktop.
Save tonylukasavage/8541502 to your computer and use it in GitHub Desktop.
This is essentially what Alloy is doing under the hood for you with backbone
// Fetch some User
var someUser = Alloy.createModel('User', {
id: '1234'
});
someUser.fetch();
// Create new Profile controller
var someProfile = Alloy.createController('Profile', { user: someUser });
// Change model
someUser.set('name', 'New Name');
var EVENTS = 'fetch change destroy';
var args = arguments || {};
// remove existing bindings to prevent unexpected behavior and memory leaks
$.user.off(EVENTS);
// add bindings to new model
args.user.on(EVENTS, function() {
$.name.text = args.user.get('name');
});
// probably want to delete the bindings on window close
$.win.addEventListener('close', function() {
args.user.off(EVENTS);
});
$.win.open();
<Alloy>
<Model src="User" id="user" instance="true" />
<Window id="win">
<Label id="name" text="{$.user.name}" />
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment