tutorial: http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/
I am going through the CloudEdit rails/backbone.js tutrorial and got stuck on the very first chunk of code. here it is:
var Document = Backbone.Model.extend({
url : function() {
var base = 'documents';
if (this.isNew()) return base;
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
}
});
the line I am struggling with is this
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
Since base = 'documents' isn’t base.charAt(base.length - 1) = s ?
I know what it is supposed to do, I am just wondering why it does it. Does the value of base change to documents/ sometimes? Why not just write return base+'/'+this.id
That line is the equivalent of
just having
return base+'/'+this.idwould result in a url of “documents//6” for a model withbase="documents/"andid=6Update
As Adam Lassek mentioned in the comments, that line of should simply be written as
for this example as the
basevariable is defined in that same block. However that line of code is also in the backbone.js source code and its purpose is as described above.Instead of overriding the
urlmethod you should set theurlRootattribute in the model: