[英]Backbone belongs_to has_many
Longtime Rails Dev, Backbone Noob. 长期Rails Dev,Backbone Noob。
In my rails models, a project has many tasks and a task belongs to a project.. Standard stuff. 在我的rails模型中,一个项目有很多任务,一个任务属于一个项目..标准的东西。
Trying to get a project's tasks json in a collection. 试图在集合中获取项目的任务json。
ExampleApp.Collections.Tasks = Backbone.Collection.extend({
url: '/projects/<dynamic_id>/tasks',
model: ExampleApp.Models.Task
});
Every example Ive seen so far references the url as /tasks. 到目前为止,我见过的每个例子都将url引用为/ tasks。 Id like to pass a project id to the collection to get that projects tasks. 我想将项目ID传递给集合以获取项目任务。
Ive checked out Backbone Relational but not sure what the best solution is. 我已经检查了Backbone Relational但不确定最佳解决方案是什么。
Cheers 干杯
I would highly recommend using Backbone-Relational as opposed to hacking the relationship on your own. 我强烈建议使用Backbone-Relational而不是自己破解关系。 Reasons for using Backbone-Relational from personal experience: 从个人经验中使用Backbone-Relational的原因:
One approach is to define url as a function and set your project_id prior to fetch() 一种方法是将url定义为函数,并在fetch()之前设置project_id
ExampleApp.Collections.Tasks = Backbone.Collection.extend({
model: ExampleApp.Models.Task
url: function() {
return 'projects/'+this.project_id+'/tasks';
},
setProjectId(project_id) {
this.project_id = project_id;
this.fetch();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.