Skip to content

Commit

Permalink
Merge branch 'master' of github.com:documentcloud/backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 26, 2011
2 parents ee305c4 + 061fd3b commit 4226d58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 7 additions & 7 deletions backbone.js
Expand Up @@ -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);
},
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 + '$');
},

Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions test/model.js
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test/test.html
Expand Up @@ -6,6 +6,9 @@
<script type="text/javascript" src="vendor/json2.js"></script>
<script type="text/javascript" src="vendor/jquery-1.6.4.js"></script>
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript">
QUnit.config.reorder = false;
</script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="vendor/underscore-1.2.2.js"></script>
<script type="text/javascript" src="../backbone.js"></script>
Expand Down

0 comments on commit 4226d58

Please sign in to comment.