Skip to content

Commit

Permalink
Merge pull request #68 from uzikilon/master
Browse files Browse the repository at this point in the history
Added events to comply with Backbone.sync
  • Loading branch information
jeromegn committed Feb 3, 2013
2 parents 3c12cf6 + 2110cac commit 2d087b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion backbone.localStorage-min.js

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

6 changes: 4 additions & 2 deletions backbone.localStorage.js
Expand Up @@ -8,11 +8,11 @@
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["underscore","backbone"], function(_, Backbone) {
// Use global variables if the locals is undefined.
// Use global variables if the locals are undefined.
return factory(_ || root._, Backbone || root.Backbone);
});
} else {
// RequireJS isn't being used. Assume underscore and backbone is loaded in <script> tags
// RequireJS isn't being used. Assume underscore and backbone are loaded in <script> tags
factory(_, Backbone);
}
}(this, function(_, Backbone) {
Expand Down Expand Up @@ -141,6 +141,7 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
}

if (resp) {
model.trigger("sync", model, resp, options);
if (options && options.success)
if (Backbone.VERSION === "0.9.10") {
options.success(model, resp, options);
Expand All @@ -154,6 +155,7 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
errorMessage = errorMessage ? errorMessage
: "Record Not Found";

model.trigger("error", model, errorMessage, options);
if (options && options.error)
if (Backbone.VERSION === "0.9.10") {
options.error(model, errorMessage, options);
Expand Down
34 changes: 34 additions & 0 deletions spec/localStorage_spec.js
Expand Up @@ -197,6 +197,16 @@ describe("Backbone.localStorage", function(){
assert.equal(Backbone.getSyncMethod(model), Backbone.localSync);
});

describe("fetch", function(){
it('should fire sync event on fetch', function(done) {
var model = new Model(attributes);
model.on('sync', function(){
done();
});
model.fetch();
});
});

describe("save", function(){

before(function(){
Expand All @@ -221,6 +231,23 @@ describe("Backbone.localStorage", function(){

});

describe('fires events', function(){
before(function(){
this.model = new Model();
});
after(function(){
this.model.destroy();
});

it('should fire sync event on save', function(done) {
this.model.on('sync', function(){
this.model.off('sync');
done();
}, this);
this.model.save({foo: 'baz'});
});
});

});

describe("destroy", function(){
Expand Down Expand Up @@ -275,6 +302,13 @@ describe("Backbone.localStorage", function(){
assert.equal(error, "Private browsing is unsupported");
});

it('should throw an error event', function(done){
model.on('error', function() {
done();
});
model.save();
});

after(function(){
window.localStorage.setItem = oldSetItem;
})
Expand Down

0 comments on commit 2d087b8

Please sign in to comment.