我正在使用 ember.js RC1 + ember-data rev 11(但我还需要一些普通的 ajax 来进行模型等配置)。我想遍历一个简单的对象列表并显示记录(注意 - 这里我只创建一个基本数组)
我绑定的内容定义了如下自定义查找方法
App.Foo = DS.Model.extend({
name: DS.attr('string')
}).reopenClass({
records: [],
all: function() {
return this.records;
},
find: function() {
var self = this;
$.getJSON('/api/foo/', function(response) {
response.forEach(function(data) {
//say I want to kill everything in the array here for some strange reason...
self.records = [];
//the template still shows the record ... not an empty list ?
}, this);
});
return this.records;
}
});
我的另一个模型直接使用这个
App.Related = DS.Model.extend({
listings: function() {
return App.Foo.find();
}.property()
});
现在在我的模板中
{{#each foo in related.listings}}
{{foo.name}}<br />
{{/each}}
默认情况下,列表会加载我放入数组中的任何内容(比如我使用 createRecord 添加一个简单的对象,就像这样)
add: function(record) {
this.records.addObject(App.Foo.createRecord(record));
},
当模板被渲染时,我看到这里列出的任何内容......但正如我在上面的评论中所说,如果我决定删除记录或将绑定的列表清空,它似乎并没有以任何方式反映这一点。
是否可以像我一样绑定一个简单的数组,然后使用诸如拼接之类的基本方法从中删除项目?甚至是激烈的 self.records = []; ?
self.records.splice(i, 1);
即使我在拼接或清空工作后手动查询客户端,它也会返回 0
console.log(App.Foo.all().get('length'));
最初我看到记录,但后来我看到它们不见了(但 html 没有改变)