In my base collection i have the base path, from the base path i am extending further urls.. but while i console the url in the extended collection i am not getting the full path of the url..
instead just i am getting the url - what the extended collection have.. why i am getting like so, and what should be the proper approach?
here is my try :
BaseCollection = Backbone.Collection.extend({
path: 'http://path/to/api'
});
TeachersCollection = BaseCollection.extend({
url:"xyz/abc",
initialize:function(){
console.log(this.url);//xyz/abc - why i am getting like this instead of full path?
//full url should be 'http://path/to/api/xyz/abc' - how can i get like this..?
}
});
var x = new TeachersCollection;
The easiest solution would be to use a function for
url
, then you could do things like this:The problem with trying to use just a string for
url
in such cases is getting the rightthis
so that you can look up thepath
. You could go throughBaseCollection.prototype
:but that's pretty cumbersome and noisy, I don't think saving the overhead of a function call is worth the hassle.
Maybe you can write this code:
path
is not a special property on any of Backbone's classesurlRoot
, but there's no such thing for collectionsHere's an approach that should work for you:
You may actually want to think about changing the constructor on your base collection, like this if you're going to be extending it a lot: