Skip to content

Commit

Permalink
Merge pull request jashkenas#1213 from braddunbar/backbone-ajax
Browse files Browse the repository at this point in the history
Fix jashkenas#474 - Backbone.ajax
  • Loading branch information
jashkenas committed Apr 11, 2012
2 parents 67c3ea8 + 87c9b17 commit a88efef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
5 changes: 4 additions & 1 deletion backbone.js
Expand Up @@ -1356,9 +1356,12 @@
} }


// Make the request, allowing the user to override any Ajax options. // 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. // Wrap an optional error callback with a fallback error event.
Backbone.wrapError = function(onError, originalModel, options) { Backbone.wrapError = function(onError, originalModel, options) {
return function(model, resp) { return function(model, resp) {
Expand Down
90 changes: 50 additions & 40 deletions index.html
Expand Up @@ -313,6 +313,7 @@
</a> </a>
<ul class="toc_section"> <ul class="toc_section">
<li><a href="#Sync">Backbone.sync</a></li> <li><a href="#Sync">Backbone.sync</a></li>
<li><a href="#Sync-ajax">Backbone.ajax</a></li>
<li><a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li> <li><a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
<li><a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li> <li><a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
</ul> </ul>
Expand Down Expand Up @@ -623,7 +624,7 @@ <h2 id="Events">Backbone.Events</h2>
object.off("change", onChange); object.off("change", onChange);


// Removes all "change" callbacks. // Removes all "change" callbacks.
object.off("change"); object.off("change");


// Removes the `onChange` callback for all events. // Removes the `onChange` callback for all events.
object.off(null, onChange); object.off(null, onChange);
Expand Down Expand Up @@ -755,7 +756,7 @@ <h2 id="Model">Backbone.Model</h2>
you may want to override <b>constructor</b>, which allows you may want to override <b>constructor</b>, which allows
you to replace the actual constructor function for your model. you to replace the actual constructor function for your model.
</p> </p>

<p> <p>
If you pass a <tt>{collection: ...}</tt> as the <b>options</b>, the model If you pass a <tt>{collection: ...}</tt> as the <b>options</b>, the model
gains a <tt>collection</tt> property that will be used to indicate which gains a <tt>collection</tt> property that will be used to indicate which
Expand Down Expand Up @@ -886,16 +887,16 @@ <h2 id="Model">Backbone.Model</h2>
<b class="header">attributes</b><code>model.attributes</code> <b class="header">attributes</b><code>model.attributes</code>
<br /> <br />
The <b>attributes</b> property is the internal hash containing the model's The <b>attributes</b> property is the internal hash containing the model's
state &mdash; usually (but not necessarily) a form of the JSON object state &mdash; usually (but not necessarily) a form of the JSON object
representing the model data on the server. It's often a straightforward 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 serialization of a row from the database, but it could also be client-side
computed state. computed state.
</p> </p>

<p> <p>
Please use <a href="#Model-set">set</a> to update the <b>attributes</b> Please use <a href="#Model-set">set</a> to update the <b>attributes</b>
instead of modifying them directly. If you'd like to retrieve and munge a instead of modifying them directly. If you'd like to retrieve and munge a
copy of the model's attributes, use <a href="#Model-toJSON">toJSON</a> copy of the model's attributes, use <a href="#Model-toJSON">toJSON</a>
instead. instead.
</p> </p>


Expand Down Expand Up @@ -932,7 +933,7 @@ <h2 id="Model">Backbone.Model</h2>


<p class="warning"> <p class="warning">
Remember that in JavaScript, objects are passed by reference, so if you 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 <b>defaults</b> as a function. Instead, define <b>defaults</b> as a function.
</p> </p>


Expand Down Expand Up @@ -1464,32 +1465,32 @@ <h2 id="Collection">Backbone.Collection</h2>
is sorted, and if your collection isn't sorted, <b>at</b> will still is sorted, and if your collection isn't sorted, <b>at</b> will still
retrieve models in insertion order. retrieve models in insertion order.
</p> </p>

<p id="Collection-push"> <p id="Collection-push">
<b class="header">push</b><code>collection.push(model, [options])</code> <b class="header">push</b><code>collection.push(model, [options])</code>
<br /> <br />
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
<a href="#Collection-add">add</a>. <a href="#Collection-add">add</a>.
</p> </p>

<p id="Collection-pop"> <p id="Collection-pop">
<b class="header">pop</b><code>collection.pop([options])</code> <b class="header">pop</b><code>collection.pop([options])</code>
<br /> <br />
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
<a href="#Collection-remove">remove</a>. <a href="#Collection-remove">remove</a>.
</p> </p>

<p id="Collection-unshift"> <p id="Collection-unshift">
<b class="header">unshift</b><code>collection.unshift(model, [options])</code> <b class="header">unshift</b><code>collection.unshift(model, [options])</code>
<br /> <br />
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
<a href="#Collection-add">add</a>. <a href="#Collection-add">add</a>.
</p> </p>

<p id="Collection-shift"> <p id="Collection-shift">
<b class="header">shift</b><code>collection.shift([options])</code> <b class="header">shift</b><code>collection.shift([options])</code>
<br /> <br />
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
<a href="#Collection-remove">remove</a>. <a href="#Collection-remove">remove</a>.
</p> </p>


Expand Down Expand Up @@ -2041,6 +2042,15 @@ <h2 id="Sync">Backbone.sync</h2>
<tt>to_json</tt> calls on models by setting <tt>ActiveRecord::Base.include_root_in_json = false</tt> <tt>to_json</tt> calls on models by setting <tt>ActiveRecord::Base.include_root_in_json = false</tt>
</p> </p>


<p id="Sync-ajax">
<b class="header">ajax</b><code>Backbone.ajax = function(settings){ ... };</code>
<br />
If you want to use a custom ajax method or your ajax method doesn't support the
<a href="http://api.jquery.com/jQuery.ajax/">jQuery.ajax</a> api and you need to
translate the options you can do so by setting <tt>Backbone.ajax</tt>. The method
you supply will be used instead of the default <tt>$.ajax</tt>.
</p>

<p id="Sync-emulateHTTP"> <p id="Sync-emulateHTTP">
<b class="header">emulateHTTP</b><code>Backbone.emulateHTTP = true</code> <b class="header">emulateHTTP</b><code>Backbone.emulateHTTP = true</code>
<br /> <br />
Expand Down Expand Up @@ -2706,16 +2716,16 @@ <h2 id="examples-soundcloud">SoundCloud Mobile</h2>
<img src="docs/images/soundcloud.png" alt="SoundCloud" class="example_image" /> <img src="docs/images/soundcloud.png" alt="SoundCloud" class="example_image" />
</a> </a>
</div> </div>

<h2 id="examples-artsy">Art.sy</h2> <h2 id="examples-artsy">Art.sy</h2>


<p> <p>
<a href="http://art.sy">Art.sy</a> is a place to discover art you'll <a href="http://art.sy">Art.sy</a> is a place to discover art you'll
love. Art.sy is built on Rails, using love. Art.sy is built on Rails, using
<a href="https://github.com/intridea/grape">Grape</a> to serve a robust <a href="https://github.com/intridea/grape">Grape</a> to serve a robust
<a href="http://art.sy/api">JSON API</a>. The main site is a single page <a href="http://art.sy/api">JSON API</a>. The main site is a single page
app written in Coffeescript and uses Backbone to provide structure around app written in Coffeescript and uses Backbone to provide structure around
this API. An admin panel and partner CMS have also been extracted into this API. An admin panel and partner CMS have also been extracted into
their own API-consuming Backbone projects. their own API-consuming Backbone projects.
</p> </p>


Expand Down Expand Up @@ -2967,18 +2977,18 @@ <h2 id="examples-decide">Decide</h2>
<img src="docs/images/decide.png" alt="Decide" class="example_image" /> <img src="docs/images/decide.png" alt="Decide" class="example_image" />
</a> </a>
</div> </div>

<h2 id="examples-editd">EDITD</h2> <h2 id="examples-editd">EDITD</h2>


<p> <p>
<a href="http://editd.com">EDITD</a> aims to disrupt the fashion <a href="http://editd.com">EDITD</a> aims to disrupt the fashion
industry with big data. The next generation of their web application industry with big data. The next generation of their web application
is based on a custom JavaScript framework that builds on top of is based on a custom JavaScript framework that builds on top of
Backbone. The back end is Backbone. The back end is
<a href="https://www.djangoproject.com/">Django</a> + <a href="https://www.djangoproject.com/">Django</a> +
<a href="http://www.elasticsearch.org/">Elastic Search</a>, <a href="http://www.elasticsearch.org/">Elastic Search</a>,
<a href="http://handlebarsjs.com/">Handlebars.js</a> is used for templating, <a href="http://handlebarsjs.com/">Handlebars.js</a> is used for templating,
<a href="http://code.google.com/p/js-test-driver/">jsTestDriver</a> for testing, and <a href="http://code.google.com/p/js-test-driver/">jsTestDriver</a> for testing, and
<a href="http://jashkenas.github.com/docco/">Docco</a> for quick documentation. <a href="http://jashkenas.github.com/docco/">Docco</a> for quick documentation.
</p> </p>


Expand Down Expand Up @@ -3363,34 +3373,34 @@ <h2 id="faq">F.A.Q.</h2>
</p> </p>


<h2 id="changelog">Change Log</h2> <h2 id="changelog">Change Log</h2>

<b class="header">0.9.2</b> &mdash; <small><i>March 21, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/backbone/compare/0.9.1...0.9.2">Diff</a><br /> <b class="header">0.9.2</b> &mdash; <small><i>March 21, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/backbone/compare/0.9.1...0.9.2">Diff</a><br />
<ul style="margin-top: 5px;"> <ul style="margin-top: 5px;">
<li> <li>
Instead of throwing an error when adding duplicate models to a collection, Instead of throwing an error when adding duplicate models to a collection,
Backbone will now silently skip them instead. Backbone will now silently skip them instead.
</li> </li>
<li> <li>
Added <a href="#Collection-push">push</a>, Added <a href="#Collection-push">push</a>,
<a href="#Collection-pop">pop</a>, <a href="#Collection-pop">pop</a>,
<a href="#Collection-unshift">unshift</a>, and <a href="#Collection-unshift">unshift</a>, and
<a href="#Collection-shift">shift</a> to collections. <a href="#Collection-shift">shift</a> to collections.
</li> </li>
<li> <li>
A model's <a href="#Model-changed">changed</a> hash is now exposed for A model's <a href="#Model-changed">changed</a> hash is now exposed for
easy reading of the changed attribute delta, since the model's last easy reading of the changed attribute delta, since the model's last
<tt>"change"</tt> event. <tt>"change"</tt> event.
</li> </li>
<li> <li>
Added <a href="#Collection-where">where</a> to collections for simple Added <a href="#Collection-where">where</a> to collections for simple
filtering. filtering.
</li> </li>
<li> <li>
You can now use a single <a href="#Events-off">off</a> call You can now use a single <a href="#Events-off">off</a> call
to remove all callbacks bound to a specific object. to remove all callbacks bound to a specific object.
</li> </li>
<li> <li>
Bug fixes for nested individual change events, some of which may be Bug fixes for nested individual change events, some of which may be
"silent". "silent".
</li> </li>
<li> <li>
Expand All @@ -3401,7 +3411,7 @@ <h2 id="changelog">Change Log</h2>
with <tt>{wait: true}</tt>. with <tt>{wait: true}</tt>.
</li> </li>
<li> <li>
Updated / refreshed the example Updated / refreshed the example
<a href="examples/todos/index.html">Todo List</a> app. <a href="examples/todos/index.html">Todo List</a> app.
</li> </li>
</ul> </ul>
Expand Down
6 changes: 3 additions & 3 deletions test/model.js
Expand Up @@ -5,7 +5,7 @@ $(document).ready(function() {
// Variable to catch ajax params. // Variable to catch ajax params.
var ajaxParams = null; var ajaxParams = null;
var sync = Backbone.sync; var sync = Backbone.sync;
var ajax = $.ajax; var ajax = Backbone.ajax;
var urlRoot = null; var urlRoot = null;


var proxy = Backbone.Model.extend(); var proxy = Backbone.Model.extend();
Expand Down Expand Up @@ -34,14 +34,14 @@ $(document).ready(function() {
}; };
sync.apply(this, arguments); sync.apply(this, arguments);
}; };
$.ajax = function(params) { ajaxParams = params; }; Backbone.ajax = function(params) { ajaxParams = params; };
urlRoot = Backbone.Model.prototype.urlRoot; urlRoot = Backbone.Model.prototype.urlRoot;
Backbone.Model.prototype.urlRoot = '/'; Backbone.Model.prototype.urlRoot = '/';
}, },


teardown: function() { teardown: function() {
Backbone.sync = sync; Backbone.sync = sync;
$.ajax = ajax; Backbone.ajax = ajax;
Backbone.Model.prototype.urlRoot = urlRoot; Backbone.Model.prototype.urlRoot = urlRoot;
} }


Expand Down
15 changes: 12 additions & 3 deletions test/sync.js
@@ -1,6 +1,6 @@
$(document).ready(function() { $(document).ready(function() {


var ajax = $.ajax var ajax = Backbone.ajax;
var lastRequest = null; var lastRequest = null;


var Library = Backbone.Collection.extend({ var Library = Backbone.Collection.extend({
Expand All @@ -18,14 +18,14 @@ $(document).ready(function() {


setup : function() { setup : function() {
library = new Library(); library = new Library();
$.ajax = function(obj) { Backbone.ajax = function(obj) {
lastRequest = obj; lastRequest = obj;
}; };
library.create(attrs, {wait: false}); library.create(attrs, {wait: false});
}, },


teardown: function() { teardown: function() {
$.ajax = ajax; Backbone.ajax = ajax;
} }


}); });
Expand Down Expand Up @@ -148,4 +148,13 @@ $(document).ready(function() {
Backbone.sync('create', model); 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);
});

}); });

0 comments on commit a88efef

Please sign in to comment.