I am trying to fetch all clients, who have nested client-contacs (people). I am having some trouble getting the client-contact collection who belongs to the client/company. If I try to get the collection I get nothing. BTW, I am new at backbone and related stuff.
Here is the code I run at the console to show my problem.
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)
if I run c.get('contacts').fetch() I get all "client-contacts" not only those who are related. Could be an URL issue? What i am missing...?
Thanks.
Here is the code of the models:
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'
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'