The example from docs about many-to-many relationship supposes that companies would be added after the person was already created.
However, what if the person data comes from server with a list of companies (companies' ids) already?
Is it possible to modify the example so that the following code (or smt. similar) would be possible:
// somewhere before we have a collection of companies defined like this:
// [{id: 1, name: 'ibm'}, {id: 2, name: 'apple'}]
// and than we do:
paul = new Person({
name: 'Paul',
jobs: [1, 2]
})
paul.get('jobs').at(0).get('name') // 'ibm'
When trying to achieve this the same way I'd do with one-to-many relations, I fail:
Companies = Backbone.Collection.extend({model: Company})
companies = new Companies([{id: 1, name: 'ibm'}, {id: 2, name: 'apple'}])
john = new Person({
name: 'John',
jobs: [1]
})
john.get('jobs').toJSON() // []
companies.get(1).get('employees').toJSON() // []
Here's the fiddle you can play with: http://jsfiddle.net/ymr5Z/