This commit is contained in:
Jeremy Ashkenas
2010-10-12 09:59:15 -04:00
parent eb5a89f17f
commit cbbba479ea
4 changed files with 82 additions and 21 deletions

View File

@@ -113,6 +113,11 @@
this._formerAttributes = this.attributes();
};
// `attributes` is aliased as `toJSON`, for use with `JSON.stringify`.
var toJSON = function() {
return _.clone(this._attributes);
};
// Attach all inheritable methods to the Model prototype.
_.extend(Backbone.Model.prototype, Backbone.Events, {
@@ -124,9 +129,8 @@
_changed : false,
// Return a copy of the model's `attributes` object.
attributes : function() {
return _.clone(this._attributes);
},
toJSON : toJSON,
attributes : toJSON,
// Default URL for the model's representation on the server -- if you're
// using Backbone's restful methods, override this to change the endpoint
@@ -272,7 +276,7 @@
if (options.success) options.success(model, resp);
};
var method = this.isNew() ? 'POST' : 'PUT';
Backbone.request(method, this, success, options.error);
Backbone.sync(method, this, success, options.error);
return this;
},
@@ -284,7 +288,7 @@
if (model.collection) model.collection.remove(model);
if (options.success) options.success(model, resp);
};
Backbone.request('DELETE', this, success, options.error);
Backbone.sync('DELETE', this, success, options.error);
return this;
}
@@ -431,7 +435,7 @@
collection.refresh(resp.models);
if (options.success) options.success(collection, resp);
};
Backbone.request('GET', this, success, options.error);
Backbone.sync('GET', this, success, options.error);
return this;
},
@@ -615,12 +619,11 @@
// * Send up the models as XML instead of JSON.
// * Persist models via WebSockets instead of Ajax.
//
Backbone.request = function(type, model, success, error) {
var data = model.attributes ? {model : JSON.stringify(model.attributes())} : {};
Backbone.sync = function(type, model, success, error) {
$.ajax({
url : model.url(),
type : type,
data : data,
data : {model : model},
dataType : 'json',
success : success,
error : error

View File

@@ -7,7 +7,7 @@
<style>
body {
font-size: 14px;
line-height: 20px;
line-height: 22px;
font-family: Helvetica Neue, Helvetica, Arial;
background: #f4f4f4 url(docs/images/background.png);
}
@@ -176,11 +176,11 @@
<li> <a href="#Collection-pluck">pluck</a></li>
<li> <a href="#Collection-Underscore-Methods">Underscore Methods (24)</a></li>
</ul>
<a class="toc_title" href="#Request">
Request
<a class="toc_title" href="#Sync">
Sync
</a>
<ul class="toc_section">
<li> <a href="#Request">Backbone.request</a></li>
<li> <a href="#Sync">Backbone.sync</a></li>
</ul>
<a class="toc_title" href="#View">
View
@@ -396,6 +396,11 @@ sidebar.promptColor();
<tt>silent</tt> is passed as an option.
</p>
<p>
If the model has a <tt>validate</tt> method, it will be validated before
the attributes are set, and no changes will occur if the validation fails.
</p>
<pre>
note.set({title: "October 12", content: "Lorem Ipsum Dolor Sit Amet..."});
@@ -413,18 +418,71 @@ note.set({title: "October 31"}, {silent: true});
<b class="header">attributes</b><code>model.attributes()</code>
<br />
Return a copy of the model's attributes. This can be used for persistence,
serialization, or for augmentation before being handed off to a view.
serialization, or for augmentation before being handed off to a view.
</p>
<pre class="runnable">
var lisa = new Backbone.Model({
firstName: "Lisa",
lastName: "Roberts"
var artist = new Backbone.Model({
firstName: "Wassily",
lastName: "Kandinsky"
});
alert(JSON.stringify(lisa.attributes()));
artist.set({birthday: "December 16, 1866"});
alert(JSON.stringify(artist.attributes()));
</pre>
<p id="Model-save">
<b class="header">save</b><code>model.save(attributes, [options])</code>
<br />
Save a model to your database (or alternative persistence layer),
by delegating to <tt>Backbone.sync</tt>. If the model has a <tt>validate</tt>
method, and validation fails, the model will not be saved. If the model
<tt>isNew()</tt>, the save will be an HTTP <tt>POST</tt>, if the model already
exists on the server, the save will be a <tt>PUT</tt>. Accepts
<tt>success</tt> and <tt>error</tt> callbacks in the options hash.
</p>
<pre class="runnable">
Backbone.sync = function(type, model) {
alert(type + " " + JSON.stringify(model));
};
var book = new Backbone.Model({
title: "The Rough Riders",
author: "Theodore Roosevelt"
});
book.save();
</pre>
<p id="Model-destroy">
<b class="header">destroy</b><code>model.destroy([options])</code>
<br />
Destroys the model on the server by delegating an HTTP <tt>DELETE</tt>
request to <tt>Backbone.sync</tt>. Accepts
<tt>success</tt> and <tt>error</tt> callbacks in the options hash.
</p>
<pre>
book.destroy({
success: function(model, response) {
...
}
});
</pre>
<h2 id="changes">Change Log</h2>
<p>

View File

@@ -4,7 +4,7 @@ $(document).ready(function() {
window.lastRequest = null;
Backbone.request = function() {
Backbone.sync = function() {
lastRequest = _.toArray(arguments);
};

View File

@@ -6,7 +6,7 @@ $(document).ready(function() {
window.lastRequest = null;
// Stub out Backbone.request...
Backbone.request = function() {
Backbone.sync = function() {
lastRequest = _.toArray(arguments);
};