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
  • Select an option

  • Save tonylukasavage/8541502 to your computer and use it in GitHub Desktop.

Select an option

Save tonylukasavage/8541502 to your computer and use it in GitHub Desktop.

Revisions

  1. tonylukasavage revised this gist Jan 21, 2014. 2 changed files with 16 additions and 11 deletions.
    23 changes: 14 additions & 9 deletions Profile.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,18 @@
    var EVENTS = 'fetch change destroy';

    var args = arguments || {};

    /**
    * Works, but you loose change listeners on the original model
    */
    $.user.set(args.user.attributes);
    // 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');
    });

    /**
    * Does not work
    */
    // $.user = args.user;
    // probably want to delete the bindings on window close
    $.win.addEventListener('close', function() {
    args.user.off(EVENTS);
    });

    $.profile.open();
    $.win.open();
    4 changes: 2 additions & 2 deletions Profile.xml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <Alloy>
    <Model src="User" id="user" instance="true" />
    <Model src="User" id="user" instance="true" />

    <Window>
    <Window id="win">
    <Label id="name" text="{$.user.name}" />
    </Window>
    </Alloy>
  2. @timanrebel timanrebel revised this gist May 27, 2013. 2 changed files with 5 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Profile.js
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@ $.user.set(args.user.attributes);
    /**
    * Does not work
    */
    $.user = args.user;
    // $.user = args.user;

    $.profile.open();
    2 changes: 2 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@ var someUser = Alloy.createModel('User', {
    });
    someUser.fetch();

    // Create new Profile controller
    var someProfile = Alloy.createController('Profile', { user: someUser });

    // Change model
    someUser.set('name', 'New Name');
  3. @timanrebel timanrebel created this gist May 24, 2013.
    11 changes: 11 additions & 0 deletions Profile.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    var args = arguments || {};

    /**
    * Works, but you loose change listeners on the original model
    */
    $.user.set(args.user.attributes);

    /**
    * Does not work
    */
    $.user = args.user;
    7 changes: 7 additions & 0 deletions Profile.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <Alloy>
    <Model src="User" id="user" instance="true" />

    <Window>
    <Label id="name" text="{$.user.name}" />
    </Window>
    </Alloy>
    9 changes: 9 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // Fetch some User
    var someUser = Alloy.createModel('User', {
    id: '1234'
    });
    someUser.fetch();

    var someProfile = Alloy.createController('Profile', { user: someUser });

    someUser.set('name', 'New Name');