骨干集集合属性(链接) [英] Backbone set collection attribute (for the url)
问题描述
我需要一个id传递到集合在URL中使用(例如/user/1234/projects.json),但我不知道如何做到这一点,一个例子将是美好的。
我的应用程序的结构的方法是在启动的集合'用户'拉和呈现,我则当点击一个用户自己的文档文件从服务器拉至新的收集和呈现在一个新的观点希望。问题是获得用户ID到文件收集给相关的URL为documents.fetch()。
想我知道了,这里是一个例子:
//在视图初始化函数
this.collection =新的文件();
this.collection.project_id = this.options.project_id;
this.collection.fetch(); //集合中
网址:函数(){
回报/项目/ API /+ this.project_id +'/文档;
}
您的用户集URL应设置为/用户。一旦这样设置,你的模型应该利用这个URL,以完成他们的任务。我相信(并非完全正),如果一个模型是一个集合中,称URL方法将返回/用户/:ID。 ':ID /用户/因此,所有典型的REST上下的功能将被使用。如果你正在尝试做的事情有关系(一个用户有多个文档),它是一种冲洗和重复。因此,对于您的文档集合(其中belogs用户是否正确?)你的URL设置为'user_instance.url /文件。
要显示一个与骨干模型一对多的关系,你会做这样的事情(升级到0.5.1骨干为urlRoot):
VAR用户= Backbone.Model.extend({
初始化:功能(){
//注意,要传递功能的URL。如果你是这是很重要
//创建不是被同步时间到服务器又一个新的用户。如果你
//不喜欢的东西:{user_url:this.url()}它不会包含ID
//尚未...并通过文档的任何同步会失败......即使你同步时间的
//用户模型!
this.docs =新的文件([] {user_url:this.url});
},
urlRoot:'/用户
});变种督= Backbone.Model.extend();VAR文档= Backbone.Collection.extend({
初始化:功能(型号,参数){
this.url =功能(){args.user_url()+'/文件'; };
}
});VAR用户=新用户([{ID:1234}]);
user.docs.fetch({成功:函数(){警报('双赢')});
I need to pass an id to a collection for use in the url (e.g. /user/1234/projects.json) but am not sure how to do this, an example would be wonderful.
The way my application is structured is on launch a collection of 'users' is pulled and rendered, I then want when a user is clicked their 'documents' are pulled from the server into a new collection and rendered in a new view. The issue is getting the user id into the documents collection to give the relevant URL for the documents.fetch().
think I've got it, here is an example:
//in the the view initialize function
this.collection = new Docs();
this.collection.project_id = this.options.project_id;
this.collection.fetch();
//in the collection
url: function() {
return '/project/api/' +this.project_id+'/docs';
}
Your user collection url should be set to /user. Once that's set, your models should utilize that url in order to do their magic. I believe (not completely positive) that if a model is in a collection, calling the 'url' method will return /user/:id. So all your typical REST-ish functionality will be utilized on '/user/:id'. If you are trying to do something with a relationship (a user has many documents) it's kind of rinse and repeat. So, for your documents collection (which belogs to user correct?) you'd set the url to 'user_instance.url/documents'.
To show a one to many relationship with a backbone model, you'd do something like this (upgrade to backbone 0.5.1 for urlRoot):
var User = Backbone.Model.extend({
initialize: function() {
// note, you are passing the function url. This is important if you are
// creating a new user that's not been sync'd to the server yet. If you
// did something like: {user_url: this.url()} it wouldn't contain the id
// yet... and any sync through docs would fail... even if you sync'd the
// user model!
this.docs = new Docs([], {user_url: this.url});
},
urlRoot: '/user'
});
var Doc = Backbone.Model.extend();
var Docs = Backbone.Collection.extend({
initialize: function(models, args) {
this.url = function() { args.user_url() + '/documents'; };
}
});
var user = new User([{id: 1234}]);
user.docs.fetch({ success: function() { alert('win') });
这篇关于骨干集集合属性(链接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!