From 87c9b17aa722fb3d6b34d5b92c564eeba5516334 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Wed, 11 Apr 2012 08:33:20 -0400 Subject: [PATCH] Fix #474 - Backbone.ajax --- backbone.js | 5 ++- index.html | 90 ++++++++++++++++++++++++++++----------------------- test/model.js | 6 ++-- test/sync.js | 15 +++++++-- 4 files changed, 69 insertions(+), 47 deletions(-) diff --git a/backbone.js b/backbone.js index 307d97513..2779935da 100644 --- a/backbone.js +++ b/backbone.js @@ -1356,9 +1356,12 @@ } // Make the request, allowing the user to override any Ajax options. - return $.ajax(_.extend(params, options)); + return Backbone.ajax(_.extend(params, options)); }; + // Set the default ajax method. + Backbone.ajax = $.ajax; + // Wrap an optional error callback with a fallback error event. Backbone.wrapError = function(onError, originalModel, options) { return function(model, resp) { diff --git a/index.html b/index.html index bffeb5eb9..f97eff340 100644 --- a/index.html +++ b/index.html @@ -313,6 +313,7 @@ @@ -623,7 +624,7 @@

Backbone.Events

object.off("change", onChange); // Removes all "change" callbacks. -object.off("change"); +object.off("change"); // Removes the `onChange` callback for all events. object.off(null, onChange); @@ -755,7 +756,7 @@

Backbone.Model

you may want to override constructor, which allows you to replace the actual constructor function for your model.

- +

If you pass a {collection: ...} as the options, the model gains a collection property that will be used to indicate which @@ -886,16 +887,16 @@

Backbone.Model

attributesmodel.attributes
The attributes property is the internal hash containing the model's - state — usually (but not necessarily) a form of the JSON object + state — usually (but not necessarily) a form of the JSON object representing the model data on the server. It's often a straightforward serialization of a row from the database, but it could also be client-side computed state.

- +

- Please use set to update the attributes - instead of modifying them directly. If you'd like to retrieve and munge a - copy of the model's attributes, use toJSON + Please use set to update the attributes + instead of modifying them directly. If you'd like to retrieve and munge a + copy of the model's attributes, use toJSON instead.

@@ -932,7 +933,7 @@

Backbone.Model

Remember that in JavaScript, objects are passed by reference, so if you - include an object as a default value, it will be shared among all instances. + include an object as a default value, it will be shared among all instances. Instead, define defaults as a function.

@@ -1464,32 +1465,32 @@

Backbone.Collection

is sorted, and if your collection isn't sorted, at will still retrieve models in insertion order.

- +

pushcollection.push(model, [options])
- Add a model at the end of a collection. Takes the same options as + Add a model at the end of a collection. Takes the same options as add.

- +

popcollection.pop([options])
- Remove and return the last model from a collection. Takes the same options as + Remove and return the last model from a collection. Takes the same options as remove.

- +

unshiftcollection.unshift(model, [options])
- Add a model at the beginning of a collection. Takes the same options as + Add a model at the beginning of a collection. Takes the same options as add.

- +

shiftcollection.shift([options])
- Remove and return the first model from a collection. Takes the same options as + Remove and return the first model from a collection. Takes the same options as remove.

@@ -2041,6 +2042,15 @@

Backbone.sync

to_json calls on models by setting ActiveRecord::Base.include_root_in_json = false

+

+ ajaxBackbone.ajax = function(settings){ ... }; +
+ If you want to use a custom ajax method or your ajax method doesn't support the + jQuery.ajax api and you need to + translate the options you can do so by setting Backbone.ajax. The method + you supply will be used instead of the default $.ajax. +

+

emulateHTTPBackbone.emulateHTTP = true
@@ -2706,16 +2716,16 @@

SoundCloud Mobile

SoundCloud - +

Art.sy

- Art.sy is a place to discover art you'll - love. Art.sy is built on Rails, using - Grape to serve a robust - JSON API. The main site is a single page - app written in Coffeescript and uses Backbone to provide structure around - this API. An admin panel and partner CMS have also been extracted into + Art.sy is a place to discover art you'll + love. Art.sy is built on Rails, using + Grape to serve a robust + JSON API. The main site is a single page + app written in Coffeescript and uses Backbone to provide structure around + this API. An admin panel and partner CMS have also been extracted into their own API-consuming Backbone projects.

@@ -2967,18 +2977,18 @@

Decide

Decide - +

EDITD

- EDITD aims to disrupt the fashion + EDITD aims to disrupt the fashion industry with big data. The next generation of their web application - is based on a custom JavaScript framework that builds on top of - Backbone. The back end is - Django + - Elastic Search, - Handlebars.js is used for templating, - jsTestDriver for testing, and + is based on a custom JavaScript framework that builds on top of + Backbone. The back end is + Django + + Elastic Search, + Handlebars.js is used for templating, + jsTestDriver for testing, and Docco for quick documentation.

@@ -3363,7 +3373,7 @@

F.A.Q.

Change Log

- + 0.9.2March 21, 2012Diff
diff --git a/test/model.js b/test/model.js index d9524b044..6357fa7a2 100644 --- a/test/model.js +++ b/test/model.js @@ -5,7 +5,7 @@ $(document).ready(function() { // Variable to catch ajax params. var ajaxParams = null; var sync = Backbone.sync; - var ajax = $.ajax; + var ajax = Backbone.ajax; var urlRoot = null; var proxy = Backbone.Model.extend(); @@ -34,14 +34,14 @@ $(document).ready(function() { }; sync.apply(this, arguments); }; - $.ajax = function(params) { ajaxParams = params; }; + Backbone.ajax = function(params) { ajaxParams = params; }; urlRoot = Backbone.Model.prototype.urlRoot; Backbone.Model.prototype.urlRoot = '/'; }, teardown: function() { Backbone.sync = sync; - $.ajax = ajax; + Backbone.ajax = ajax; Backbone.Model.prototype.urlRoot = urlRoot; } diff --git a/test/sync.js b/test/sync.js index 492448132..8ef8bdedf 100644 --- a/test/sync.js +++ b/test/sync.js @@ -1,6 +1,6 @@ $(document).ready(function() { - var ajax = $.ajax + var ajax = Backbone.ajax; var lastRequest = null; var Library = Backbone.Collection.extend({ @@ -18,14 +18,14 @@ $(document).ready(function() { setup : function() { library = new Library(); - $.ajax = function(obj) { + Backbone.ajax = function(obj) { lastRequest = obj; }; library.create(attrs, {wait: false}); }, teardown: function() { - $.ajax = ajax; + Backbone.ajax = ajax; } }); @@ -148,4 +148,13 @@ $(document).ready(function() { Backbone.sync('create', model); }); + test("Backbone.ajax", 1, function() { + Backbone.ajax = function(settings){ + strictEqual(settings.url, '/test'); + }; + var model = new Backbone.Model(); + model.url = '/test'; + Backbone.sync('create', model); + }); + });