Backbone.js 如何在主干关系数据库中获取嵌套集合
我正在尝试获取所有具有嵌套客户端contacs(people)的客户端。我在获取属于客户/公司的客户联系人集合时遇到一些问题。如果我想得到收藏品,我什么也得不到。顺便说一句,我是新的骨干和相关的东西 下面是我在控制台上运行的代码,以显示我的问题Backbone.js 如何在主干关系数据库中获取嵌套集合,backbone.js,backbone-relational,Backbone.js,Backbone Relational,我正在尝试获取所有具有嵌套客户端contacs(people)的客户端。我在获取属于客户/公司的客户联系人集合时遇到一些问题。如果我想得到收藏品,我什么也得不到。顺便说一句,我是新的骨干和相关的东西 下面是我在控制台上运行的代码,以显示我的问题 c = new SpencerGrafica.Models.Client({id:1}) c.fetch() c.toJSON() Object {id: 1, name: "Name", contacts: Array[0], …} c.get('co
c = new SpencerGrafica.Models.Client({id:1})
c.fetch()
c.toJSON()
Object {id: 1, name: "Name", contacts: Array[0], …}
c.get('contacts').toJSON()
[] # (There should be ONE result, as I set this relation in rails console)
如果我运行c.get('contacts').fetch(),我将获得所有“客户联系人”,而不仅仅是那些相关的联系人。可能是URL问题?我错过了什么
谢谢
以下是模型的代码:
client.js.coffee
class SpencerGrafica.Models.Client extends Backbone.RelationalModel
paramRoot: 'client'
urlRoot: 'clients'
defaults:
id: null
name: null
relations: [{
type: Backbone.HasMany,
key: 'contacts',
relatedModel: 'SpencerGrafica.Models.ClientContact',
collectionType: 'SpencerGrafica.Collections.ClientContactsCollection',
autoFetch: true,
reverseRelation: {
key: 'client',
keySource: 'client_id'
}
}]
class SpencerGrafica.Collections.ClientsCollection extends Backbone.Collection
model: SpencerGrafica.Models.Client
url: '/clients'
class SpencerGrafica.Models.ClientContact extends Backbone.RelationalModel
paramRoot: 'client_contact'
urlRoot: 'client_contacts'
defaults:
name: null
email: null
phone: null
class SpencerGrafica.Collections.ClientContactsCollection extends Backbone.Collection
model: SpencerGrafica.Models.ClientContact
url: 'client_contacts'
ClientContact.js.coffee
class SpencerGrafica.Models.Client extends Backbone.RelationalModel
paramRoot: 'client'
urlRoot: 'clients'
defaults:
id: null
name: null
relations: [{
type: Backbone.HasMany,
key: 'contacts',
relatedModel: 'SpencerGrafica.Models.ClientContact',
collectionType: 'SpencerGrafica.Collections.ClientContactsCollection',
autoFetch: true,
reverseRelation: {
key: 'client',
keySource: 'client_id'
}
}]
class SpencerGrafica.Collections.ClientsCollection extends Backbone.Collection
model: SpencerGrafica.Models.Client
url: '/clients'
class SpencerGrafica.Models.ClientContact extends Backbone.RelationalModel
paramRoot: 'client_contact'
urlRoot: 'client_contacts'
defaults:
name: null
email: null
phone: null
class SpencerGrafica.Collections.ClientContactsCollection extends Backbone.Collection
model: SpencerGrafica.Models.ClientContact
url: 'client_contacts'
我也面临着类似的问题,目前还没有答案。但我可以和你分享一些想法 我猜您的json结构是:
/clients/: {id: 1, name: "Name"}
/client_contacts/: [{id: 1, client: 1}, {id: 2, client: 1}]
然后您需要将/clients/
更改为{id:1,name:“name”,contacts:[1,2]}
,让主干关系找出关系
另一个问题是,您使用/client\u contacts
作为客户端联系人集合
的url,这就是为什么您找回了所有联系人,因为/client\u contacts
是所有联系人的请求。
你们很多人都想了解详情
如果您不想在
/clients/
中包含联系人id,那么我们将面临相同的问题:我也面临类似的问题,目前还没有答案。但我可以和你分享一些想法
我猜您的json结构是:
/clients/: {id: 1, name: "Name"}
/client_contacts/: [{id: 1, client: 1}, {id: 2, client: 1}]
然后您需要将/clients/
更改为{id:1,name:“name”,contacts:[1,2]}
,让主干关系找出关系
另一个问题是,您使用/client\u contacts
作为客户端联系人集合
的url,这就是为什么您找回了所有联系人,因为/client\u contacts
是所有联系人的请求。
你们很多人都想了解详情
如果您不想在
/clients/
中包含联系人id,那么我们将面临相同的问题:我最终从主干关系更改为这种方法,到目前为止效果非常好。我最终从主干关系更改为这种方法,到目前为止效果非常好。