Skip to content

Commit

Permalink
New Backbone adapter that doesn't override default Backbone.sync
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte authored and tofumatt committed Feb 22, 2014
1 parent 1f1fd71 commit ff38f31
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 61 deletions.
70 changes: 46 additions & 24 deletions dist/backbone.localforage.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,35 +172,57 @@
}
});

// Override Backbone.sync to call our custom offline sync only. Delegates to
// the original Backbone.sync if offlineStore isn't defined.
var superSync = Backbone.sync;
Backbone.sync = function(method, model, options) {
var store = model.offlineStore || (model.collection && model.collection.offlineStore);

if (store) {
switch (method) {
case "read":
return model.id ? store.find(model, options) : store.findAll(options);
break;
case "create":
return store.create(model, options);
break;
case "update":
return store.update(model, options);
break;
case "delete":
return store.destroy(model, options);
break;
}
function localforageSync(store, method, model, options) {
switch (method) {
case "read":
return model.id ? store.find(model, options) : store.findAll(options);
break;
case "create":
return store.create(model, options);
break;
case "update":
return store.update(model, options);
break;
case "delete":
return store.destroy(model, options);
break;
}

return superSync.apply(this, arguments);
};

// For now, we aren't complicated: just set a property off Backbone to
// serve as our export point.
Backbone.localforage = OfflineStore;
Backbone.localforage = {

OfflineStore: OfflineStore,

sync: function(name) {
var sync, offlineStore;

// If a name's given we create a store for the model or collection;
// otherwise we assume it's a model and try to use its collection's store
if (name){
offlineStore = new OfflineStore(name);
sync = function(method, model, options) {
return localforageSync.apply(null, [offlineStore].concat([].slice.call(arguments, 0)));
}
sync.offlineStore = offlineStore;
} else {
sync = function(method, model, options) {
var offlineStore = model.collection && model.collection.sync.offlineStore;
if (offlineStore){
return localforageSync.apply(null, [offlineStore].concat([].slice.call(arguments, 0)));
}
// It relies on Backbone.sync if the model isn't in a collection or
// the collection doesn't have a store
return Backbone.sync.apply(this, arguments);
};
}

return sync;
}

};

return OfflineStore;

}).call(this);
2 changes: 1 addition & 1 deletion dist/backbone.localforage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 46 additions & 24 deletions src/adapters/backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,35 +172,57 @@
}
});

// Override Backbone.sync to call our custom offline sync only. Delegates to
// the original Backbone.sync if offlineStore isn't defined.
var superSync = Backbone.sync;
Backbone.sync = function(method, model, options) {
var store = model.offlineStore || (model.collection && model.collection.offlineStore);

if (store) {
switch (method) {
case "read":
return model.id ? store.find(model, options) : store.findAll(options);
break;
case "create":
return store.create(model, options);
break;
case "update":
return store.update(model, options);
break;
case "delete":
return store.destroy(model, options);
break;
}
function localforageSync(store, method, model, options) {
switch (method) {
case "read":
return model.id ? store.find(model, options) : store.findAll(options);
break;
case "create":
return store.create(model, options);
break;
case "update":
return store.update(model, options);
break;
case "delete":
return store.destroy(model, options);
break;
}

return superSync.apply(this, arguments);
};

// For now, we aren't complicated: just set a property off Backbone to
// serve as our export point.
Backbone.localforage = OfflineStore;
Backbone.localforage = {

OfflineStore: OfflineStore,

sync: function(name) {
var sync, offlineStore;

// If a name's given we create a store for the model or collection;
// otherwise we assume it's a model and try to use its collection's store
if (name){
offlineStore = new OfflineStore(name);
sync = function(method, model, options) {
return localforageSync.apply(null, [offlineStore].concat([].slice.call(arguments, 0)));
}
sync.offlineStore = offlineStore;
} else {
sync = function(method, model, options) {
var offlineStore = model.collection && model.collection.sync.offlineStore;
if (offlineStore){
return localforageSync.apply(null, [offlineStore].concat([].slice.call(arguments, 0)));
}
// It relies on Backbone.sync if the model isn't in a collection or
// the collection doesn't have a store
return Backbone.sync.apply(this, arguments);
};
}

return sync;
}

};

return OfflineStore;

}).call(this);
11 changes: 1 addition & 10 deletions test/test.backbone.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ casper.test.begin "Testing Backbone data adapter", (test) ->
test.info "Testing using global scope (no require.js)"

test.assertEval ->
typeof Backbone.localforage is 'function'
typeof Backbone.localforage is 'object'
, "localforage storage adapter is attached to Backbone.localforage"

casper.then ->
Expand All @@ -17,15 +17,6 @@ casper.test.begin "Testing Backbone data adapter", (test) ->
Models.add michael
michael.save()

test.assertRaises ->
OnlineModel = Backbone.Model.extend()

OnlineModelCollection = Backbone.Collection.extend
model: OnlineModel

bob = new OnlineModel()
, [], 'Backbone.Sync throws an error when no offlineStore or URL is specified'

casper.reload()

casper.then ->
Expand Down
6 changes: 4 additions & 2 deletions test/test.backbone.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
<script>
localforage.setDriver('localStorageWrapper');

var Model = Backbone.Model.extend();
var Model = Backbone.Model.extend({
sync: Backbone.localforage.sync()
});

var ModelCollection = Backbone.Collection.extend({
model: Model,
offlineStore: new Backbone.localforage('Models')
sync: Backbone.localforage.sync('Models')
});

var Models = new ModelCollection();
Expand Down

0 comments on commit ff38f31

Please sign in to comment.