[Pkg-javascript-commits] [backbone] 62/101: ...

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:58:29 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.1.0
in repository backbone.

commit cbbba479ea6e5df9539d9030b8b6f20781d07e70
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Tue Oct 12 09:59:15 2010 -0400

     ...
---
 backbone.js        | 21 ++++++++-------
 index.html         | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------
 test/collection.js |  2 +-
 test/model.js      |  2 +-
 4 files changed, 82 insertions(+), 21 deletions(-)

diff --git a/backbone.js b/backbone.js
index 5d786d4..05c7770 100644
--- a/backbone.js
+++ b/backbone.js
@@ -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
diff --git a/index.html b/index.html
index 8b40b73..ccad1b6 100644
--- a/index.html
+++ b/index.html
@@ -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,17 +418,70 @@ 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>
 
diff --git a/test/collection.js b/test/collection.js
index cfd27a6..67aa1ba 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -4,7 +4,7 @@ $(document).ready(function() {
 
   window.lastRequest = null;
 
-  Backbone.request = function() {
+  Backbone.sync = function() {
     lastRequest = _.toArray(arguments);
   };
 
diff --git a/test/model.js b/test/model.js
index c279766..0bd9138 100644
--- a/test/model.js
+++ b/test/model.js
@@ -6,7 +6,7 @@ $(document).ready(function() {
   window.lastRequest = null;
 
   // Stub out Backbone.request...
-  Backbone.request = function() {
+  Backbone.sync = function() {
     lastRequest = _.toArray(arguments);
   };
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list