【问题标题】:In Backbone how can I remove models from a collection based on a filter/query?在 Backbone 中,如何根据过滤器/查询从集合中删除模型?
【发布时间】:2013-10-21 11:38:38
【问题描述】:

我正在使用带有钛合金的 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
}

我的 Jasmine 测试看起来像这样

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();
      });
 });

【问题讨论】:

    标签: backbone.js jasmine titanium-alloy


    【解决方案1】:

    遍历您的集合或通过下划线使用一些辅助函数,然后调用删除函数并传入您的模型。在此处查看文档:http://backbonejs.org/#Collection-remove

    【讨论】:

      【解决方案2】:

      您也可以使用下划线的_.filter 方法::

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

      看这个小提琴:

      http://jsfiddle.net/Y3gPJ/

      【讨论】:

      • 感谢您的回复 - 我如何引用来自 removeComplete 的集合?我正在为this.fetchComplete() 获取undefined 我分叉了您的代码并进行了一些更改以使用where 方法。它不像我预期的那样工作。你能告诉我我做错了什么吗?我是 Backbone 的新手jsfiddle.net/8QFDP/1
      • 哪里是下划线方法,因此它应该用_.where 调用,你错过了_
      • 因为removeComplete 是该集合的一个函数,通过this 您可以获得当前Backbone.Collection 对象的引用
      • 对不起,如果我有点厚,但where 不是一个收集方法吗? docs.appcelerator.com/backbone/0.9.2/#Collection-where。如果this 是当前集合,它是否已经使用我的自定义函数进行了扩展,即fetchComplete
      • 是的,你是对的,还有一个名为 where 的 Backbone 方法,在这个小提琴中(基于你的代码)我可以访问 fetchComplete 方法(如预期的那样): jsfiddle.net/GungP
      猜你喜欢
      • 2016-01-08
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2013-03-05
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多