GithubHelp home page GithubHelp logo

alexanderdickson / backbone-deferred Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arhea/backbone-deferred

0.0 2.0 0.0 9.82 MB

An implementation of Backbone Model and Collection using the promise pattern rather than passing callback options. Supports popular deferred libraries like Q and jQuery.

License: MIT License

backbone-deferred's Introduction

##Backbone Deferred Build Status Built with Grunt

This library converts models and collections to use the deferred pattern. So instead of returning the raw jQuery XHR object from a fetch, save, or destroy. This library allows you to access all the same functionality provided by the success and error callbacks with the nice deferred pattern.

model.save();

###Examples

var model = new Model({ id: 1 });

model.fetch({
    success: function(model, response, options) {
        alert('Yay!');
    },
    error: function(model, xhr, options) {
        alert('Darn!');
    }
});

Becomes

var model = new Model({ id: 1 });

model.fetch().done(function(result) {
    alert('Yay!');
}).fail(function(error) {
    alert('Darn!');
});

or

var model = new Model({ id: 1 });

model.fetch().then(function(result) {
    alert('Yay!');
}, function(error) {
    alert('Darn!');
});

###Documentation Backbone.Deferred supports both the jQuery promise implementation and the Q promise implementation. I will use .done and .fail in the documentation but for Q these are one method, .then(doneCallback, failCallback). In order to be compliant across libaries the first parameter is an array which contains all the paramters passed to the Backbone callback.

Backbone.Deferred.Model.fetch(options)

  • .done(result)
    • result.model - instance of the model
    • result.response - the response from the server
    • result.options - the options passed to the function
  • .fail(error)
    • error.model - instance of the model
    • error.xhr - the jQXhr object
    • error.options - the options passed to the function

Backbone.Deferred.Model.save(key, val, options)

  • .done(result)
    • result.model - instance of the model
    • result.response - the response from the server
    • result.options - the options passed to the function
  • .fail(error)
    • error.model - instance of the model
    • error.xhr - the jQXhr object
    • error.options - the options passed to the function

Backbone.Deferred.Model.save(attributes, options)

  • .done(result)
    • result.model - instance of the model
    • result.response - the response from the server
    • result.options - the options passed to the function
  • .fail(error)
    • error.model - instance of the model
    • error.xhr - the jQXhr object
    • error.options - the options passed to the function

Backbone.Deferred.Model.destroy(options)

  • .done(result)
    • result.model - instance of the model
    • result.response - the response from the server
    • result.options - the options passed to the function
  • .fail(error)
    • error.model - instance of the model
    • error.xhr - the jQXhr object
    • error.options - the options passed to the function

Backbone.Deferred.Collection.fetch(options)

  • .done(result)
    • result.collection - instance of the collection
    • result.response - the response from the server
    • result.options - the options passed to the function
  • .fail(error)
    • result.collection - instance of the collection
    • result.xhr - the jQXhr object
    • result.options - the options passed to the function

###Requirements

  • Backbone
  • Underscore
  • jQuery

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.