ohc*_*ibi 8 ember.js ember-data
我有这个小Ember应用程序:
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'));
};
Run Code Online (Sandbox Code Playgroud)
我得到了正确的标题console.dir(App.Model.find(1).get('title')但两个get('length')电话都返回0.我错过了什么?
这是一个(非)工作的jsbin:http://jsbin.com/uxalap/6/edit
原因可能是您get("length")在加载数据之前正在调用,
基本上当你这样做时App.Model.find()会返回一个实例,RecordArray但是它没有数据,在后台查询数据库并将获取数据,现在加载数据后你会发现实际的长度
您可以尝试在isLoaded属性上添加Observer ,如下所示
record = App.store.findQuery(App.Model);
alertCount = function(){
if(record.isLoaded){
alert(record.get("length"));
}
};
Ember.addObserver("isLoaded", record, alertCount);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4413 次 |
| 最近记录: |