diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58b805f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +node_modules/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6570fda --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "0.8" \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..825ff7c --- /dev/null +++ b/Gruntfile.js @@ -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']); \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/backbone-sync-deferred.js b/backbone-sync-deferred.js new file mode 100644 index 0000000..09aad26 --- /dev/null +++ b/backbone-sync-deferred.js @@ -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; + +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3d1a2e5 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/tests/data.json b/tests/data.json new file mode 100644 index 0000000..3873a55 --- /dev/null +++ b/tests/data.json @@ -0,0 +1,3 @@ +{ + "some":"data" +} \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js new file mode 100644 index 0000000..fa4e7ee --- /dev/null +++ b/tests/tests.js @@ -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(); + }); + }); + + + }) +}) \ No newline at end of file