使用collection.url BackboneJS model.url [英] BackboneJS model.url using collection.url

查看:190
本文介绍了使用collection.url BackboneJS model.url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的理解骨干JS模式的默认行为是返回集合的URL,除非的模型有一个 urlRoot 指定。我似乎无法获得工作的行为。

From my understanding the default behavior of Backbone JS Models are to return the Collection's URL, unless the model has a urlRoot specified. I can't seem to get the behavior to work.

从文档:

model.url()...生成形式的URL:[collection.url] / [ID]在默认情况下,但你可以通过指定一个明确的urlRoot如果模型的收藏不应该被改写成帐户。

model.url() ... Generates URLs of the form: "[collection.url]/[id]" by default, but you may override by specifying an explicit urlRoot if the model's collection shouldn't be taken into account.

下面是我收集和型号分别为:

Here is my collection, and model respectively:

var MyCollection = Backbone.Collection.extend({
    model: Model,
    initialize: function(options){
        this.options = options || {};
    },
    url: function(){
        return "/theurl/" + this.options.param;
    }
});
return MyCollection;

...

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

当一个模型没有一个集合加载,它的伟大工程,并提交给 / theurl ,但是当它的加载到一个集合,所有的方法提交给 / theurl /参数/

When a model is loaded without a collection, it works great and submits to /theurl, but when it's loaded into a collection, all methods submit to /theurl/param/.

如果我理解正确的文件,在 urlRoot 模型应覆盖此行为的;即使如此,该机型的网址应该是 / theurl /参数/ {MODEL-ID}

If I'm understanding the documentation correctly, the urlRoot of the Model should override this behavior; and even then the models url should be /theurl/param/{MODEL-ID}.

这是我缺少的是什么/误解任何想法?

Any ideas on what I'm missing/misunderstanding?

...

PS:在型号:型号从集合是通过RequireJS带来了

PS: The model: Model from the collection is brought in via RequireJS

推荐答案

它总是会使用收集的网址,即使你有指定的 urlRoot

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

原因 urlRoot 是这样,你可以用它替代时,或者如果模型恰好不是一个集合中(例如,也许它被删除,但仍然存在在客户端上)。

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).

所以,如果你想保存模型,并覆盖由收集你会生成的URL需要通过 urlRoot 进入这些方法明确地作为一个选项。例如:

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.

这篇关于使用collection.url BackboneJS model.url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆