diff --git a/backbone.js b/backbone.js index d30b00753..28d1cf7e2 100644 --- a/backbone.js +++ b/backbone.js @@ -292,7 +292,7 @@ // using Backbone's restful methods, override this to change the endpoint // that will be called. url : function() { - var base = getValue(this.collection, 'url') || this.urlRoot || urlError(); + var base = getValue(this.collection, 'url') || getValue(this, 'urlRoot') || urlError(); if (this.isNew()) return base; return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id); }, @@ -638,8 +638,8 @@ // Cached regular expressions for matching named param parts and splatted // parts of route strings. - var namedParam = /:([\w\d]+)/g; - var splatParam = /\*([\w\d]+)/g; + var namedParam = /:\w+/g; + var splatParam = /\*\w+/g; var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g; // Set up all inheritable **Backbone.Router** properties and methods. @@ -687,9 +687,9 @@ // Convert a route string into a regular expression, suitable for matching // against the current location hash. _routeToRegExp : function(route) { - route = route.replace(escapeRegExp, "\\$&") - .replace(namedParam, "([^\/]*)") - .replace(splatParam, "(.*?)"); + route = route.replace(escapeRegExp, '\\$&') + .replace(namedParam, '([^\/]*)') + .replace(splatParam, '(.*?)'); return new RegExp('^' + route + '$'); }, @@ -855,7 +855,7 @@ // a new one to the browser history. _updateHash: function(location, fragment, replace) { if (replace) { - location.replace(location.toString().replace(/(javascript:|#).*$/, "") + "#" + fragment); + location.replace(location.toString().replace(/(javascript:|#).*$/, '') + '#' + fragment); } else { location.hash = fragment; } diff --git a/test/model.js b/test/model.js index 85c6602ca..0ef49c30b 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); diff --git a/test/test.html b/test/test.html index 7dda5b331..743292c1d 100644 --- a/test/test.html +++ b/test/test.html @@ -6,6 +6,9 @@ +