如何在收集和使用模型骨干动态URL [英] how to create dynamic url in collection and model using backbone
问题描述
我的采集和模型是这样的:
My collection and model like this:
detail_userid = 0;
detail_contactid = 0;
var ContactDetail = Backbone.Model.extend({
urlRoot: URL_CONTACTS1+detail_userid+"/"+detail_contactid
});
var ContactDetailCollection = Backbone.Collection.extend({
model: ContactDetail,
url: URL_CONTACTS1+detail_userid+"/"+detail_contactid
})
入口是:
ContactDetailManagePageModel.prototype.init = function(m,n){
detail_userid = m;
detail_contactid = n;
var myContactDetails = new ContactDetailCollection();
var contactDetailListView = new ContactDetailListView({
collection: myContactDetails
});
myContactDetails.fetch({reset:true});
}
但它运行时,网址为:的http://本地主机:8080 / WS /用户/联系人/ 0/0
,这意味着分配给detail_userid和detail_contactid不成功,我不知道为什么。
But when it runs,the url is :http://localhost:8080/ws/users/contacts/0/0
,it means that the assignment to detail_userid and detail_contactid is unsuccessful,I don't know why.
希望对你help.Thanks。
Hope for your help.Thanks.
推荐答案
我觉得你是静态definining的urlRoot和url属性正在运行的PageModel(不太清楚的init之前,你在哪里得到m和n从虽然...)
I think you are statically definining the urlRoot and url properties before you are running the init of the PageModel (not quite sure where you are getting m and n from though...)
这两个网址,urlRoot可以是函数,所以你可以在实例化过程中传递的选项,让他们在模型上动态地设置。
Both url and urlRoot can be a function, so you can pass in options during instantiation and have them dynamically set on the model.
简单的例子覆盖定义集合,然后创建有一个
Simple example covering defining the collection and then creating one
var ContactDetailCollection = Backbone.Collection.extend({
model: ContactDetail,
url: function(){
return URL_CONTACTS1 + this.options.detail_userid + "/" + this.options.detail_contactid;
}
});
var myContactDetails = new ContactDetailCollection({
detail_userid: foo,
detail_contactid: bar
});
正如我所说,我不知道你的初始化函数是干什么的,我猜它是从您的应用程序定制的东西,我并不需要担心。
As I mentioned, I'm not sure what your init function is doing, I'm guessing it's something custom from your app that I don't need to worry about.
我相当肯定带走主要的是设置URL和urlRoot动态
I'm fairly sure the main thing to take away is to set url and urlRoot dynamically
这篇关于如何在收集和使用模型骨干动态URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!