我正在使用带有钛合金的 Backbonejs 0.9.2,我需要从集合中删除所有已完成的任务。Backbone.sync 配置为使用 SQLite 本地数据库。
extendCollection: function(Collection) {
exts = {
self: this
, fetchComplete: function() {
var table = definition.config.adapter.collection_name
this.fetch({query:'SELECT * from ' + table + ' where complete=1'})
}
, removeComplete: function () {
this.remove(this.fetchComplete())
}
}
_.extend(Collection.prototype, exts);
return Collection
}
我的茉莉花测试看起来像这样
describe("task model", function () {
var Alloy = require("alloy"),
data = {
taskId: 77
},
collection,
item;
beforeEach(function(){
collection = Alloy.createCollection('task');
item = Alloy.createModel('task');
});
// PASSES
it('can fetch complete tasks', function(){
item.set(data);
item.save();
collection.fetchComplete();
expect(0).toEqual(collection.length);
item.markAsComplete();
item.save();
collection.fetchComplete();
expect(1).toEqual(collection.length);
});
// FAILS
it('can remove completed tasks', function(){
// we have 6 items
collection.fetch()
expect(6).toEqual(collection.length);
// there are no completed items
collection.fetchComplete();
expect(0).toEqual(collection.length);
item.set(data);
item.save();
item.markAsComplete();
item.save();
// we have 7 items 1 of which is complete
collection.fetch()
expect(7).toEqual(collection.length);
collection.removeComplete()
// after removing the complete item we should have 6 left
collection.fetch()
expect(6).toEqual(collection.length);
});
afterEach(function () {
item.destroy();
});
});