ianhaggerty / backbone-sails

A plugin for Backbone that makes integrating with Sails JS easier. Both leverage a model-view architecture, it's only natural to have them talk to each other.
MIT License
7 stars 1 forks source link

Using addTo returns error #1

Closed dottodot closed 9 years ago

dottodot commented 9 years ago

Firstly thank you for what looks to be an amazing plugin.

Just having a little difficulty using the addTo feature. I'm trying to add and existing model to another model which has a many to many association.

membersListView.on("itemview:user:add", function(childview, model) {
                      addingUser = team.addTo("users", model);
                      addingUser.done(function(teamData) {
                        console.log(teamData);
                      });
});

but I get an error of Uncaught TypeError: Cannot read property 'users' of undefined

team has already been fetch and users populated.

I'm not sure if misinterpreting to use of addTo or doing something wrong.

ianhaggerty commented 9 years ago

Hello

Thanks for your interest in my plugin - good to see it getting noticed a little!

To make use of the association functionality, you'll need to declare the associations as part of your Backbone.Sails.Model definition:

var Team = Backbone.Sails.Model.extend({
  assoc: {
    users: UserCollection
  }
})

assoc keeps reference to the constructor's used to wrap raw data given and received by the API. You can also pass a string (the modelName), or an array of a string to indicate an associated collection:

var Team = Backbone.Sails.Model.extend({
  assoc: {
    users: ['user'] // associated collection with modelName of `user`,
    captain: 'user' // associated model
  }
})

(You can also pass a function taking no arguments returning a Model or Collection for circular references)

If that doesn't solve the problem, could you please send me some more info. A stack trace would be ideal, along with your model definitions.

dottodot commented 9 years ago

That's great, went with the second option and that work perfectly.