BackboneJS model.url using collection.url

江枫思渺然 提交于 2019-12-01 15:51:52

It will always use the collection's url even if you have urlRoot specified.

The reason for urlRoot is so you can use it in an override, or if the model happens to not be in a collection ( for example maybe it gets removed, but still exists on the client).

So if you want to fetch or save the model and override the url generated by the collection you'll need to pass the urlRoot into these methods explicitly as an option. For example:

yourModel.save({ url: yourModel.urlRoot });

I agree the documentation is confusing and this caught me out recently too.

UrlRoot should be a function and model must have attributeId defined. If you define your model like this all operation will be working if model is in collection or not.

Backbone add modelId at the end of URL that is returned by urlRoot function.

var MyModel = Backbone.Model.extend({
    attributeId: 'myModelId'
    urlRoot: function() {
        return '/theurl';
    },
    initialize: function() {
    }
    defaults: {
        myModelId: null
    }
});
brownmamba

In the model, try using:

url: function() {
    return 'your url goes here';
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!