Backbone.Safe
Backbone.Safe copied to clipboard
Safe not working correctly for Backbone Collections
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);
hi @Langtonm. can you post an example of where "safe" doesn't work for a collection?
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);
});
});
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 )));
},