1

クライアントの連絡先 (人) がネストされているすべてのクライアントを取得しようとしています。クライアント/会社に属するクライアント連絡先コレクションを取得するのに問題があります。コレクションを取得しようとしても、何も取得できません。ところで、私はバックボーンと関連するものは初めてです。

私の問題を示すためにコンソールで実行するコードを次に示します。

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'
4

1 に答える 1

1

私は同様の問題に直面しており、まだ答えがありません。でも、あなたと何か考えを共有できるかもしれません。

あなたのjson構造は次のとおりだと思います:

/clients/: {id: 1, name: "Name"}
/client_contacts/: [{id: 1, client: 1}, {id: 2, client: 1}]

次に、backbone-relational が関係を把握/clients/できるように変更する必要があります。{id: 1, name: "Name", contacts: [1, 2]}

もう 1 つの問題は/client_contacts、 の URL として使用することです。これが、すべての連絡先に対する要求でClientContactsCollectionあるため、すべての連絡先を取得した理由です。詳細については、 http://backbonerelational.org/#example-person/client_contactsを確認してください。

に連絡先 ID を含めたくない場合は/clients/、同じ問題に直面しています。

于 2013-04-11T22:04:21.697 に答える