0

私は Backbonejs 0.9.2 と Titanium Alloy を使用しており、完了したすべてのタスクをコレクションから削除する必要があります。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();
      });
 });
4

2 に答える 2

0

または、アンダースコアのメソッドを使用できます_.filter::

_.filter(tasks, function (task) {
    return task.status == 'ACTIVE'
});

このフィドルを参照してください:

http://jsfiddle.net/Y3gPJ/

于 2013-10-21T12:01:54.890 に答える