-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9ba7800
Showing
8 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- "0.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"some":"data" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
|
||
|
||
}) | ||
}) |