I have this little Ember application:
window.App = Ember.Application.create();
App.Store = DS.Store.extend({
revision: 11,
adapter: DS.FixtureAdapter({
simulateRemoteResponse: false
})
});
App.Model = DS.Model.extend({
title: DS.attr('string')
});
App.Model.FIXTURES = [];
App.ready = function() {
console.dir(App.Model.find().get('length'));
App.Model.createRecord({id: 1, title: "TEST"});
console.dir(App.Model.find().get('length'));
console.dir(App.Model.find(1).get('title'));
};
I get the right title in console.dir(App.Model.find(1).get('title')
however both of the get('length')
calls return 0. What am I missing?
Here is a (non-) working jsbin: http://jsbin.com/uxalap/6/edit
The reason might be that you are invoking get("length")
even before the data has been loaded,
Basically when you do App.Model.find()
it returns you an instance of RecordArray
but it wont have data, in the background it queries with the database and will fetch the data, now once the data is loaded you'll find the actual length
you might try adding an Observer on isLoaded
property as follows
record = App.store.findQuery(App.Model);
alertCount = function(){
if(record.isLoaded){
alert(record.get("length"));
}
};
Ember.addObserver("isLoaded", record, alertCount);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With