[Pkg-javascript-commits] [backbone] 25/34: Adding Backbone.Model#fetch

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


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

js pushed a commit to tag 0.1.2
in repository backbone.

commit 17d64e0a51a2b84d8a6accfb67cf00719526d4dc
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Mon Oct 18 21:31:27 2010 -0400

    Adding Backbone.Model#fetch
---
 backbone.js   | 14 ++++++++++++++
 test/model.js |  6 ++++++
 test/sync.js  |  7 +++++++
 3 files changed, 27 insertions(+)

diff --git a/backbone.js b/backbone.js
index c0217e3..d77ba84 100644
--- a/backbone.js
+++ b/backbone.js
@@ -186,6 +186,20 @@
       return value;
     },
 
+    // Fetch the model from the server. If the server's representation of the
+    // model differs from its current attributes, they will be overriden,
+    // triggering a `"change"` event.
+    fetch : function(options) {
+      options || (options = {});
+      var model = this;
+      var success = function(resp) {
+        if (!model.set(resp.model)) return false;
+        if (options.success) options.success(model, resp);
+      };
+      Backbone.sync('read', this, success, options.error);
+      return this;
+    },
+
     // Set a hash of model attributes, and sync the model to the server.
     // If the server returns an attributes hash that differs, the model's
     // state will be `set` again.
diff --git a/test/model.js b/test/model.js
index f1c99a4..82a5345 100644
--- a/test/model.js
+++ b/test/model.js
@@ -107,6 +107,12 @@ $(document).ready(function() {
     ok(_.isEqual(lastRequest[1], doc));
   });
 
+  test("Model: fetch", function() {
+    doc.fetch();
+    ok(lastRequest[0], 'read');
+    ok(_.isEqual(lastRequest[1], doc));
+  });
+
   test("Model: destroy", function() {
     doc.destroy();
     equals(lastRequest[0], 'delete');
diff --git a/test/sync.js b/test/sync.js
index ae1d994..36888b1 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -68,6 +68,13 @@ $(document).ready(function() {
     equals(data.length, 123);
   });
 
+  test("sync: read model", function() {
+    library.first().fetch();
+    equals(lastRequest.url, '/library/2-the-tempest');
+    equals(lastRequest.type, 'GET');
+    ok(_.isEmpty(lastRequest.data));
+  });
+
   test("sync: destroy", function() {
     Backbone.emulateHttp = false;
     library.first().destroy();

-- 
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