クライアントの連絡先 (人) がネストされているすべてのクライアントを取得しようとしています。クライアント/会社に属するクライアント連絡先コレクションを取得するのに問題があります。コレクションを取得しようとしても、何も取得できません。ところで、私はバックボーンと関連するものは初めてです。
私の問題を示すためにコンソールで実行するコードを次に示します。
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.コーヒー
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'
ClientContact.js.coffee
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'