removing Backbone.Model#isEqual

This commit is contained in:
Jeremy Ashkenas
2010-10-12 11:01:38 -04:00
parent d66f9208b9
commit c14dde7c20
3 changed files with 28 additions and 18 deletions

View File

@@ -32,6 +32,11 @@
return child;
};
// Get a url as a property or as a function.
var getUrl = function(object) {
return _.isFunction(object.url) ? object.url() : object.url;
};
// Backbone.Events
// -----------------
@@ -136,7 +141,7 @@
// using Backbone's restful methods, override this to change the endpoint
// that will be called.
url : function() {
var base = this.collection.url();
var base = getUrl(this.collection);
if (this.isNew()) return base;
return base + '/' + this.id;
},
@@ -152,11 +157,6 @@
return new (this.constructor)(this.attributes());
},
// Are this model's attributes identical to another model?
isEqual : function(other) {
return other && _.isEqual(this._attributes, other._attributes);
},
// A model is new if it has never been saved to the server, and has a negative
// ID.
isNew : function() {
@@ -621,7 +621,7 @@
//
Backbone.sync = function(type, model, success, error) {
$.ajax({
url : model.url(),
url : getUrl(model),
type : type,
data : {model : model},
dataType : 'json',

View File

@@ -167,6 +167,7 @@
<li> <a href="#Collection-getIds">getIds</a></li>
<li> <a href="#Collection-at">at</a></li>
<li> <a href="#Collection-sort">sort</a></li>
<li> <a href="#Model-url">url</a></li>
<li> <a href="#Collection-refresh">refresh</a></li>
<li> <a href="#Collection-fetch">fetch</a></li>
<li> <a href="#Collection-create">create</a></li>
@@ -510,8 +511,26 @@ one.set({
});
</pre>
<p id="Model-url">
<b class="header">url</b><code>model.url()</code>
<br />
Returns the relative URL where the model's resource would be located on
the server. If your models are located somewhere else, override this method
with the correct logic. Generates URLs of the form: <tt>"/[collection]/[id]"</tt>.
</p>
<p>
A model with an id of <tt>101</tt>, stored in a
<tt>Bindable.Collection</tt> with a <tt>url</tt> of <tt>"/notes"</tt>,
would have this URL: <tt>"/notes/101"</tt>
</p>
<p id="Model-clone">
<b class="header">clone</b><code>model.clone()</code>
<br />
Create a new instance of a model with identical attributes.
</p>

View File

@@ -54,15 +54,6 @@ $(document).ready(function() {
equals(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
});
test("model: isEqual", function() {
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
a = new Backbone.Model(attrs);
b = new Backbone.Model(attrs);
ok(a.isEqual(b), "a should equal b");
c = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'qux': 4});
ok(!a.isEqual(c), "a should not equal c");
});
test("model: isNew", function() {
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
a = new Backbone.Model(attrs);