Ember Data (List from JSON)
by besign
HTML
<script src="http://lab.besign.com.ve/eurocrm/app/data/test.json"></script>
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js"></script>
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-latest.js"></script>
<script src="http://cloud.github.com/downloads/emberjs/data/ember-data-latest.js"></script>
<script type="text/x-handlebars">
{{#each App.personController}}
<p>{{name}} {{surname}}</p>
{{/each}}
</script>
JavaScript
App = Em.Application.create();
App.Person = DS.Model.extend({
name: DS.attr('string'),
surname: DS.attr('string'),
}).reopenClass({
url: 'test.json'
});
App.adapter = DS.Adapter.create({
findAll: function (store, type) {
console.log('finding');
var url = type.url;
// $.getJSON(url, function(data) {
// store.loadMany(type, data);
// });
var data = [
{"name": "Bruce","surname": "Banner"},
{"name": "Peter","surname": "Parker"},
{"name": "Natasha","surname": "Romanoff"},
{"name": "Steve","surname": "Rogers"},
{"name": "Tony","surname": "Stark"}
];
store.loadMany(type, data);
console.log ('loading');
},
createRecord: function (store, type) {
console.log ('creating');
//if function is empty, why is it loading the new name?
}
});
App.store = DS.Store.create({
revision: 4,
adapter: App.adapter
});
App.personController = Ember.ArrayController.create({
content: [],
init: function(){
this._super();// create an instance of the person model
this.set('content', App.store.findAll(App.Person));
var anotherhero = App.store.createRecord(App.Person, { 'name': 'Bruce', 'surname': 'Wayne' });
},
});