Backbone.Safe icon indicating copy to clipboard operation
Backbone.Safe copied to clipboard

Safe not working correctly for Backbone Collections

Open Langtonm opened this issue 12 years ago • 3 comments

I am trying to use the Backbone.Safe from a Backbone.Collection but the Backbone.Safe does not appear to function correctly (I get a item in my collection, after a safe.reload(), when it should be empty).

It looks like then 'isCollection' value is not being determined correctly and as a result the code is treating my collection as a model.

I've modified the code as follows and the Backbone.Safe then works as expected :-

// this.isCollection = !context.set && context.models && context.add; this.isCollection = ((typeof context.add === 'function') && context.models!=null);

Langtonm avatar Nov 05 '13 12:11 Langtonm

hi @Langtonm. can you post an example of where "safe" doesn't work for a collection?

orizens avatar Nov 05 '13 12:11 orizens

describe("Collection Suite", function(){ beforeEach(function(){ SafeCollection = Backbone.Collection.extend({ safe: "collection-safe" }); });

    it("given empty collection and safe when reloaded then the collection should remain empty", function(){
        collection = new SafeCollection();
        collection.safe.reload();
        expect( 0 ).toEqual( collection.length);
    });
});

Langtonm avatar Nov 05 '13 14:11 Langtonm

Other changes I made to get collections working are :- .... var collection = {

        // events that Safe is listening in order to
        // trigger save to local storage
        events: 'add remove reset change',

.....

    store: function(model) { 
        this.storage()
            .setItem(this.uid, JSON.stringify( this.toJSON( this.context )));
    },

Langtonm avatar Nov 05 '13 15:11 Langtonm