Skip to content

Commit

Permalink
Fix jashkenas#474 - Backbone.ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Apr 11, 2012
1 parent 67c3ea8 commit 87c9b17
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.
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) {
Expand Down
90 changes: 50 additions & 40 deletions index.html
Expand Up @@ -313,6 +313,7 @@
</a>
<ul class="toc_section">
<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-emulateJSON">Backbone.emulateJSON</a></li>
</ul>
Expand Down Expand Up @@ -623,7 +624,7 @@ <h2 id="Events">Backbone.Events</h2>
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);
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 to replace the actual constructor function for your model.
</p>

<p>
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
Expand Down Expand Up @@ -886,16 +887,16 @@ <h2 id="Model">Backbone.Model</h2>
<b class="header">attributes</b><code>model.attributes</code>
<br />
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
serialization of a row from the database, but it could also be client-side
computed state.
</p>

<p>
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
copy of the model's attributes, use <a href="#Model-toJSON">toJSON</a>
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
copy of the model's attributes, use <a href="#Model-toJSON">toJSON</a>
instead.
</p>

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

<p class="warning">
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.
</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
retrieve models in insertion order.
</p>

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

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

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

<p id="Collection-shift">
<b class="header">shift</b><code>collection.shift([options])</code>
<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>.
</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>
</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">
<b class="header">emulateHTTP</b><code>Backbone.emulateHTTP = true</code>
<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" />
</a>
</div>

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

<p>
<a href="http://art.sy">Art.sy</a> is a place to discover art you'll
love. Art.sy is built on Rails, using
<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
app written in Coffeescript and uses Backbone to provide structure around
this API. An admin panel and partner CMS have also been extracted into
<a href="http://art.sy">Art.sy</a> is a place to discover art you'll
love. Art.sy is built on Rails, using
<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
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.
</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" />
</a>
</div>

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

<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
is based on a custom JavaScript framework that builds on top of
Backbone. The back end is
<a href="https://www.djangoproject.com/">Django</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://code.google.com/p/js-test-driver/">jsTestDriver</a> for testing, and
is based on a custom JavaScript framework that builds on top of
Backbone. The back end is
<a href="https://www.djangoproject.com/">Django</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://code.google.com/p/js-test-driver/">jsTestDriver</a> for testing, and
<a href="http://jashkenas.github.com/docco/">Docco</a> for quick documentation.
</p>

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

<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 />
<ul style="margin-top: 5px;">
<li>
Instead of throwing an error when adding duplicate models to a collection,
Backbone will now silently skip them instead.
</li>
<li>
Added <a href="#Collection-push">push</a>,
<a href="#Collection-pop">pop</a>,
<a href="#Collection-unshift">unshift</a>, and
<a href="#Collection-shift">shift</a> to collections.
Added <a href="#Collection-push">push</a>,
<a href="#Collection-pop">pop</a>,
<a href="#Collection-unshift">unshift</a>, and
<a href="#Collection-shift">shift</a> to collections.
</li>
<li>
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
<tt>"change"</tt> event.
</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.
</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.
</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".
</li>
<li>
Expand All @@ -3401,7 +3411,7 @@ <h2 id="changelog">Change Log</h2>
with <tt>{wait: true}</tt>.
</li>
<li>
Updated / refreshed the example
Updated / refreshed the example
<a href="examples/todos/index.html">Todo List</a> app.
</li>
</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.
var ajaxParams = null;
var sync = Backbone.sync;
var ajax = $.ajax;
var ajax = Backbone.ajax;
var urlRoot = null;

var proxy = Backbone.Model.extend();
Expand Down Expand Up @@ -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;
}

Expand Down
15 changes: 12 additions & 3 deletions 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({
Expand All @@ -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;
}

});
Expand Down Expand Up @@ -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);
});

});

0 comments on commit 87c9b17

Please sign in to comment.