From 6ccdbb3a60c575f8dd1a672ff2b60994a8657417 Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Tue, 20 Dec 2011 18:18:08 -0500 Subject: [PATCH 1/5] Optimize regular expressions. The capture groups aren't needed/used; the character classes will do the job just fine. Also, normalize the use of quotation-marks (change `"` to `'`' where appropriate). --- backbone.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backbone.js b/backbone.js index eafa08a22..f4ad577b8 100644 --- a/backbone.js +++ b/backbone.js @@ -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\d]+/g; + var splatParam = /\*[\w\d]+/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; } From 9e80ca1457d4999472633ef7d91c23a26d05c992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20M=2E=20Costa?= Date: Wed, 21 Dec 2011 01:29:50 -0200 Subject: [PATCH 2/5] \w still includes \d http://www.regular-expressions.info/charclass.html#shorthand --- backbone.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backbone.js b/backbone.js index f4ad577b8..a74453b96 100644 --- a/backbone.js +++ b/backbone.js @@ -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. From 9d145abfe83f750d4f15d8d0d198b13a4e2c021b Mon Sep 17 00:00:00 2001 From: Irene Ros Date: Thu, 22 Dec 2011 12:23:50 -0500 Subject: [PATCH 3/5] Forcing quint test execution order. --- test/test.html | 3 +++ 1 file changed, 3 insertions(+) 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 @@ + From b4e650b98f8176c8ee62d505383620ab9000b8ab Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 22 Dec 2011 15:23:12 -1000 Subject: [PATCH 4/5] 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 a74453b96..cec63a1d6 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 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); From 93a39110defdd22caf60d4221441390541872852 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 22 Dec 2011 17:19:07 -1000 Subject: [PATCH 5/5] Use getValue to interpret urlRoot instead. --- backbone.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backbone.js b/backbone.js index cec63a1d6..edb6943a5 100644 --- a/backbone.js +++ b/backbone.js @@ -292,8 +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(); - if (typeof(base) == 'function') base = base.call(this); // allow urlRoot to be determined at runtime + 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); },