I'm currently using backbone, backbone-relational, and jQuery for a PhoneGap project that is using web-sql storage.
I'm trying to pull a related model object from the parent object using Model.get('key'), but when I do that it returns a related model object with only the id
attribute set - all other attributes are non-existent.
In my GameModel, I have defined a relationship as follows:
type: Backbone.HasOne,
key: "game_type_id",
relatedModel: GamePropertiesModel,
includeInJSON: true
... 3 more related models ...
In my view I'm looping through each relationship of my GameModel and lazy-loading the related models using Backbone.RelationalModel.fetchRelated()
as follows:
_.each(self.model.getRelations(), function(relation) {
dfds = dfds.concat(self.model.fetchRelated(relation.key));
})
When I call:
console.log( self.model.get("game_type_id") )
OR
console.log( self.model.attributes['game_type_id'] )
I get a GamePropertiesModel, but the only value set is ID. All other attributes are not listed.
Contrarily, if I call:
console.log( self.model )
I can drill down via GameModel.attributes.game_type_id.attributes and see the full list of attributes with values set.
Any ideas why this is happening? I need to access these the related models of GameModel to be able to display their attribute values in my view/template.
UPDATE 2013-08-12:
The solution to the above was an error in my $.when call and the objects were being outputted by console.log
prior to the SQL query actually running. This has been corrected with:
$.when.apply(this, dfds).done(...)
Now I am experiencing a loading problem due to backbone-relational using a Store to ensure only one instance of a Model object exists. Basically I am rendering to the view 4 times to append an item to a list, but the last item in the view is returning before the others with empty related objects. This is because prior list items are loading the same related objects and have marked in the Store that it has been loaded, but in reality the query hasn't been executed yet -- It's just an empty instance that hasn't been loaded.
I need some way to loop through my list sequentially so that this doesn't happen. Any suggestions?