[Pkg-javascript-commits] [backbone] 134/281: A model's urlRoot can now be a function allowing definition at runtime.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:04 UTC 2014


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

js pushed a commit to tag 0.9.0
in repository backbone.

commit b4e650b98f8176c8ee62d505383620ab9000b8ab
Author: = <=>
Date:   Thu Dec 22 15:23:12 2011 -1000

    A model's urlRoot can now be a function allowing definition at runtime.
---
 backbone.js   |  1 +
 test/model.js | 12 ++++++++++++
 2 files changed, 13 insertions(+)

diff --git a/backbone.js b/backbone.js
index a74453b..cec63a1 100644
--- a/backbone.js
+++ b/backbone.js
@@ -293,6 +293,7 @@
     // that will be called.
     url : function() {
       var base = getValue(this.collection, 'url') || this.urlRoot || urlError();
+      if (typeof(base) == 'function') base = base.call(this); // allow urlRoot to be determined at runtime
       if (this.isNew()) return base;
       return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
     },
diff --git a/test/model.js b/test/model.js
index 85c6602..0ef49c3 100644
--- a/test/model.js
+++ b/test/model.js
@@ -87,6 +87,18 @@ $(document).ready(function() {
     equals(model.url(), '/collection/%2B1%2B');
   });
 
+  test("Model: url when using urlRoot as a function to determine urlRoot at runtime", function() {
+    var Model = Backbone.Model.extend({
+      urlRoot: function() { return '/nested/' + this.get('parent_id') + '/collection'}
+      // looks better in coffeescript: urlRoot: => "/nested/#{@get('parent_id')}/collection"
+    });	
+
+    var model = new Model({parent_id: 1});
+    equals(model.url(), '/nested/1/collection');
+    model.set({id: 2});
+    equals(model.url(), '/nested/1/collection/2');
+  });
+
   test("Model: clone", function() {
     attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
     a = new Backbone.Model(attrs);

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