处理与Backbone.js的一个belongs_to的关系 [英] Handling a belongs_to relation with backbone.js
问题描述
我是那种与Backbone.js的新的和我与获取一个集合了belong_to关系中挣扎。
我想获取用户列表(用户/ GET),我想以显示它们但有belongs_to的关联。
例如,显示用户的公司名称。有人知道一个解决方案吗?
我使用的Ruby on使用CouchDB的Rails 3
用户控制器
高清指数
User.all
结束
骨干集合
类UserCollection扩展Backbone.Collection 网址: - >
app.routes.users_url 模式:用户
骨干模型
类用户扩展Backbone.Model idAttribute:_id 默认值:{
电子邮件:空
手机:空
禄:空
} 网址: - >
app.routes.users_url +'/'+(@id ||'')
我想显示的公司名称像这样...
%{脚本:ID => 用户资源模板:类型=> 文/模板}
%TD = check_box_tag选择,1,假的:class => 复选框,数据ID=>原始(&下;%= _id%gt;中)
%TD<%=名称%GT;
%TD<%=电子邮件%GT;
%TD&下;%= company.name%GT;
Backbone.js的似乎不支持关系,因为有在的官方文档。
所有不丢失,不过,也有一些骨干插件,增加对关系的支持,如 Backbone-关系或 ligament.js 。我没有测试过他们,但骨干关系似乎pretty活跃。
更新
要延迟加载的集合和prevent每个关系一个取动作,请参阅 fetchRelated
方法的 Backbone.RelationalModel :
获取从模型中引用的服务器型号
属性,但尚未发现/创建呢。这可以用于
专为懒加载场景。
在默认情况下,一个独立的请求将被每增加发射模式
要被从服务器获取。但是,如果你的服务器/ API
支持它,你可以在一个请求所获取的模型集
指定用于调用fetchRelated上的关系的collectionType。
该collectionType应该有一个覆盖
URL(型号)方法,允许它来构建一个网址
对于模型的数组。看在顶部的例子
Backbone.Relation选项或<一个href=\"https://github.com/PaulUithol/backbone-tastypie/blob/master/backbone_tastypie/static/js/backbone-tastypie.js#L79\"相对=nofollow>骨干-tastypie 一个例子。
块引用>I am kind of new with backbone.js and i am struggling with fetching a collection with a belong_to relation.
I would like to fetch a list of users (users/ GET) and i would like to display them all but with a belongs_to association.
For example to display the company name of the user. Somebody knows a solution?
I am using Ruby on Rails 3 with CouchDB
users controller
def index User.all end
Backbone collection
class UserCollection extends Backbone.Collection url: -> app.routes.users_url model: User
Backbone model
class User extends Backbone.Model idAttribute: '_id' defaults: { "email": null "mobile": null "loc": null } url: -> app.routes.users_url + '/' + (@id || '')
I would like to show the company name like this...
%script{:id => "user-resource-template", :type => "text/template"} %td= check_box_tag "select", 1, false, :class => "checkbox", "data-id" => raw("<%= _id %>") %td <%= name %> %td <%= email %> %td <%= company.name %>
解决方案Backbone.js doesn't seem to support relations, as there is no mention of them in the official documentation.
All is not lost, though, there are some Backbone plugins that add support for relations, such as Backbone-relational or ligament.js. I haven't tested them, but Backbone-relational seems pretty active.
Update
To lazy-load collections and prevent one fetch per relation, see the
fetchRelated
method in Backbone.RelationalModel:Fetch models from the server that were referenced in the model's attributes, but have not been found/created yet. This can be used specifically for lazy-loading scenarios.
By default, a separate request will be fired for each additional model that is to be fetched from the server. However, if your server/API supports it, you can fetch the set of models in one request by specifying a collectionType for the relation you call fetchRelated on. The collectionType should have an overridden url(models) method that allows it to construct a url for an array of models. See the example at the top of Backbone.Relation options or Backbone-tastypie for an example.
这篇关于处理与Backbone.js的一个belongs_to的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!