Skip to content

Commit

Permalink
Sync methods are now testable and testes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Proulx committed Mar 26, 2012
1 parent e11448c commit 8cafed1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
24 changes: 15 additions & 9 deletions backbone.localStorage.js
Expand Up @@ -84,12 +84,6 @@ _.extend(Backbone.LocalStorage.prototype, {
Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options, error) {
var store = model.localStorage || model.collection.localStorage;

// if there's no localStorage property, we can assume that we want to use the default Backbone sync method.
if(!store) {
Backbone.ajaxSync(method, model, options, error);
return;
}

// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
Expand All @@ -114,9 +108,21 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
}
};

// Override 'Backbone.sync' to default to localSync,
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
Backbone.ajaxSync = Backbone.sync;
Backbone.sync = Backbone.LocalStorage.sync;

Backbone.getSyncMethod = function(model) {
if(model.localStorage || (model.collection && model.collection.localStorage))
{
return Backbone.localSync;
}

return Backbone.ajaxSync;
};

// Override 'Backbone.sync' to default to localSync,
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
Backbone.sync = function(method, model, options, error) {
Backbone.getSyncMethod(model).apply(this, [method, model, options, error]);
};

})();
18 changes: 17 additions & 1 deletion tests/test.js
Expand Up @@ -142,5 +142,21 @@ $(document).ready(function() {
book.destroy()
equals(Book.prototype.localStorage.findAll().length, 0, 'book removed');
});


test("Book should use local sync", function()
{
var method = Backbone.getSyncMethod(book);
equals(method, Backbone.localSync);
});

var MyRemoteModel = Backbone.Model.extend();

var remoteModel = new MyRemoteModel();

test("remoteModel should use ajax sync", function()
{
var method = Backbone.getSyncMethod(remoteModel);
equals(method, Backbone.ajaxSync);
});

});

0 comments on commit 8cafed1

Please sign in to comment.