Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
spacenick committed Jun 13, 2013
0 parents commit 9ba7800
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules/
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.8"
11 changes: 11 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
grunt.loadNpmTasks('grunt-contrib-concat');

module.exports = function(grunt) {
uglify:{
dist:{
'backbone-sync-deferred.min.js': ['backbone-sync-deferred.js']
}
}
};

grunt.registerTask('build', ['concat', 'uglify']);
Empty file added README.md
Empty file.
47 changes: 47 additions & 0 deletions backbone-sync-deferred.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
// CommonJS support for easier testing
(function(root,factory, undefined){
if (module !== undefined) {
// CommonJS
module.exports = factory(
require('backbone'),
require('jquery')
);
} else {
// Browser mode. Just inject manually the dependencies that should be defined in
// the global window scope.
root.Backbone.sync = factory(root.Backbone, root.$);
}
})(this,function(Backbone, $){

// Keep track of original sync
var origSync = Backbone.sync;

var newSync = function(method, model, options) {

// Keep track of original callbacks to have a seamless integration
var origSuccess = options.success;
var origError = options.error;

// Let's get a nice promise
var deferred = $.Deferred();

// Override success
options.success = function(model, response, options) {
if (origSuccess) origSuccess(model, response, options);
return deferred.resolve(model);
};
// Override error
options.error = function(model, response, options) {
if (origError) origError(model, response, options);
return deferred.reject(response.responseText);
};

origSync(method, model, options);

return deferred.promise;
};

return newSync;

});
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "backbone-sync-deferred",
"version": "0.0.1",
"description": "Override Backbone.sync for a lightweight and seamless integration of promise with Backbone",
"main": "backbone-sync-deferred.bundle.min.js",
"scripts": {
"test": "mocha tests/tests.js --reporter nyan",
"postinstall": "npm install -g mocha"
},
"repository": "",
"keywords": [
"backbone",
"deferred",
"promise",
"asynchronous"
],
"author": "Nicolas Kermarc <@spacenick>",
"license": "MIT",
"devDependencies": {
"chai": "~1.6.1",
"backbone": "~1.0.0",
"jquery": "~1.8.3",
"grunt": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-uglify": "~0.2.2"
}
}
3 changes: 3 additions & 0 deletions tests/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"some":"data"
}
54 changes: 54 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var assert = require("chai").assert;
var Backbone = require("backbone")
var BackboneSyncDeferred = require('../backbone-sync-deferred.js');
var $ = require('jquery');

Backbone.$ = $;

describe('Backbone Model sync overrided', function(){
describe('fetch', function(){



it('should return a promise', function(done){
var model = new Backbone.Model();
model.url = "https://api.github.com/users/spacenick";
var p = model.fetch();
assert.typeOf(p.then, 'function');
p.then(function(data){
assert.typeOf(data, 'object');
done();
});
});


it('should trigger the error handler', function(done){
var model = new Backbone.Model();
// Let's have a 404 url
model.url = "https://api.github.com/users/spacenic";
var p = model.fetch();
assert.typeOf(p.then, 'function');
p.then(function(data){
assert.typeOf(data, 'object');
done();
},
function(error){
done();
});
});

it('should seamlessly execute the default fetch callback', function(done){
var model = new Backbone.Model();
// Let's have a 404 url
model.url = "https://api.github.com/users/spacenick";
var p = model.fetch();
assert.typeOf(p.then, 'function');
p.then(function(data){
assert.equal(model.get('login'), 'spacenick');
done();
});
});


})
})

0 comments on commit 9ba7800

Please sign in to comment.