[Pkg-javascript-commits] [backbone] 29/101: Adding Backbone.Collection#fetch

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:58:26 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 1074f247cd0ac301fdd29729e1ae4f038765839c
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Tue Oct 5 11:58:29 2010 -0400

    Adding Backbone.Collection#fetch
---
 backbone.js | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/backbone.js b/backbone.js
index f741971..8db4e17 100644
--- a/backbone.js
+++ b/backbone.js
@@ -66,6 +66,7 @@
           calls[ev] = [];
         } else {
           var list = calls[ev];
+          if (!list) return this;
           for (var i = 0, l = list.length; i < l; i++) {
             if (callback === list[i]) {
               list.splice(i, 1);
@@ -265,16 +266,14 @@
   // Provides a standard collection class for our sets of models, ordered
   // or unordered. If a `comparator` is specified, the Collection will maintain
   // its models in sort order, as they're added and removed.
-  Backbone.Collection = function(models,options) {
+  Backbone.Collection = function(models, options) {
     if (options && options.comparator) {
       this.comparator = options.comparator;
       delete options.comparator;
     }
     this._boundOnModelEvent = _.bind(this._onModelEvent, this);
     this._initialize();
-    if (models) {
-      this.refresh(models,true);
-    }
+    if (models) this.refresh(models,true);
   };
 
   // Define the Collection's inheritable methods.
@@ -375,6 +374,18 @@
       if (!silent) this.trigger('refresh');
     },
 
+    // Fetch the default set of models for this collection, refreshing the
+    // collection.
+    fetch : function(options) {
+      options || (options = {});
+      var collection = this;
+      var success = function(resp) {
+        collection.refresh(resp.models);
+        if (options.success) options.success(collection, resp);
+      };
+      Backbone.request('GET', this, success, options.error);
+    },
+
     // Create a new instance of a model in this collection.
     create : function(model, options) {
       options || (options = {});
@@ -527,10 +538,11 @@
 
   // `Backbone.request`...
   Backbone.request = function(type, model, success, error) {
+    var data = model.attributes ? {model : JSON.stringify(model.attributes())} : {};
     $.ajax({
       url       : model.url(),
       type      : type,
-      data      : {model : JSON.stringify(model.attributes())},
+      data      : data,
       dataType  : 'json',
       success   : success,
       error     : error

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