diff --git a/backbone.js b/backbone.js index a74453b9..cec63a1d 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 85c6602c..0ef49c30 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);