Created
April 15, 2012 07:35
-
-
Save rickard2/2390740 to your computer and use it in GitHub Desktop.
EmberJS Data handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.App = Ember.Application.create({}); | |
Item = Ember.Object.extend({ | |
}); | |
App.ListController = Ember.ArrayController.create({ | |
content: [], | |
currentItem: null, | |
// Runs when the user clicks any of the items in the list | |
itemClick: function(event) { | |
var $target = $(event.target || event.srcElement), | |
id = $target.data('itemId'); | |
for (var i = 0; i < this.content.length; i++) { | |
if ( this.content[i].id == id ) { | |
this.set('currentItem', this.content[i]); | |
} | |
} | |
App.DetailController.load(); | |
}, | |
init: function() { | |
// load data with ajax call | |
}, | |
// Runs when the ajax call is done | |
xhrLoadHandler: function(data) { | |
var items = []; | |
for (var i = 0; i < data.length; i++) { | |
items.push( Item.create(data[i]) ); | |
} | |
App.ListController.pushObjects( items ); | |
App.stateManager.goToState('list'); | |
} | |
}); | |
App.DetailController = Ember.Object.create({ | |
dataBinding: 'App.ListController.currentItem', | |
load: function() { | |
App.stateManager.goToState('detail'); | |
}, | |
back: function() { | |
App.stateManager.goToState('list'); | |
} | |
}); | |
App.stateManager = Ember.StateManager.create({ | |
rootElement: '#main', | |
list: Ember.ViewState.create({ | |
view: Ember.View.create({ | |
templateName: 'list' | |
}) | |
}), | |
detail: Ember.ViewState.create({ | |
view: Ember.View.create({ | |
templateName: 'detail', | |
dataBinding: 'App.DetailController.data' | |
}) | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment