0

I am using Backbonejs 0.9.2 with Titanium Alloy and I need to remove all the completed tasks from a Collection. Backbone.sync is configured to use a SQLite local database.

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
}

My Jasmine tests look like this

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();
      });
 });
Peter
  • 4,493
  • 6
  • 41
  • 64

2 Answers2

0

Iterate through your collection or use some of the helper functions via underscore and then call the remove function and pass in your model. See the docs here: http://backbonejs.org/#Collection-remove

DanyZift
  • 647
  • 1
  • 7
  • 11
0

Alternatively you can use underscores' _.filter method::

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

See this fiddle:

http://jsfiddle.net/Y3gPJ/

Akos K
  • 7,071
  • 3
  • 33
  • 46
  • Thanks for your response - how do I reference a collection from `removeComplete`? I am getting `undefined` for `this.fetchComplete()` I forked your code and made some changes to use the `where` method. It doesn't work like I expect. Could you please tell me what I am doing wrong? I am new to Backbone http://jsfiddle.net/8QFDP/1/ – Peter Oct 21 '13 at 13:38
  • where is an underscore method therefore it should be called with `_.where`, you're missing the `_` – Akos K Oct 21 '13 at 13:55
  • since `removeComplete` is a function of that collection, by `this` you can get reference for the current Backbone.Collection object – Akos K Oct 21 '13 at 13:59
  • Sorry if I am being a bit thick but isn't `where` a Collection method? http://docs.appcelerator.com/backbone/0.9.2/#Collection-where. And if `this` is the current collection hasn't it already been extended with my custom functions i.e. `fetchComplete` – Peter Oct 21 '13 at 14:14
  • yes, you're correct there is also a Backbone method called `where`, in this fiddle (which is based on your code) I have access to the `fetchComplete` method (as expected): http://jsfiddle.net/GungP/ – Akos K Oct 21 '13 at 14:29
  • ok thanks for your help. `this.remove(this.fetchComplete())` doesn't seem to work and I don't see why :-) – Peter Oct 21 '13 at 15:04
  • as you have seen in the fiddle it's called correctly, I guess you problem is int the `fetchComplete` method. – Akos K Oct 22 '13 at 07:27