backbone.js - How to fetch the model in a view with a dynamic url defined in a view -
i using backbone.js in app. model named mymodel is
backbone.model.extend({ urlroot: 'home' });
need fetch model url "home/abc/xyz" "abc" , "xyz" dynamic in view. did following
var model = new mymodel({id:'abc/xyz'}); model.fetch();
but not working. goes url "home/abc?xyz".
how should solve issue?
here url
function of backbone.model responsible such kind of behavior in backbone:
url: function() { var base = _.result(this, 'urlroot') || _.result(this.collection, 'url') || urlerror(); if (this.isnew()) return base; return base.replace(/([^\/])$/, '$1/') + encodeuricomponent(this.id); }
as can see encodeuricomponent(this.id)
encode id, can't pass , '/' -> '%2f'. can rewrite function, guess it's not best idea, cause can break things.
i can suggest approach:
just define urlroot
of model function , there job:
var yourmodel = backbone.model.extend({ defaulturl: 'home', urlroot: function () { return defaulturl + this.get('yoururlattribute'); } });
just pass yoururlattribute
model attribute when creating , fetch model.
having in mind approach , backbone.model append encoded 'id' last part of url (when fetching model) can want.
Comments
Post a Comment