I am trying to set up the polymorphic association in Batman as documented here. I am using LocalStorage and keep getting the following message:
Related model undefined for polymorphic association not found.
My model is a follows:
class App1.Model extends Batman.Model
@persist Batman.LocalStorage
class App1.Superpower extends App1.Model
@resourceName: 'superpower'
@encode 'id', 'name'
@belongsTo 'superpowerable', polymorphic: true
class App1.Hero extends App1.Model
@resourceName: 'hero'
@encode 'id', 'name'
@hasMany 'superpowers', as: 'superpowerable', polymorphic: true
class App1.Villain extends App1.Model
@resourceName: 'villain'
@encode 'id', 'name'
@hasMany 'superpowers', as: 'superpowerable', polymorphic: true
and the code I use to instantiate all:
superman = new App1.Hero(name: "Superman")
superman.save()
super_strength = new App1.Superpower(name: "Super Strength")
super_strength.save() # -> gives "Related model undefined for polymorphic association not found."
invincibility = new App1.Superpower(name: "Invincibility")
invincibility.save() # -> gives "Related model undefined for polymorphic association not found."
superman.get('superpowers').add super_strength
superman.get('superpowers').add invincibility
superman.save()
super_strength.save()
invincibility.save()
console.log superman.toJSON()
console.log super_strength.toJSON()
console.log invincibility.toJSON()
According to this question it depends on the namespace, but they are all the same in my code, so I'm really wondering what is going wrong here.
Thanks in advance
Uh oh... I wrote that example so if it doesn't work, I'm to blame :)
Just to be sure, did you call
App1.run()
at the beginning of that script? I ran into that issue while testing last week -- gotta callrun
to load associations!Edit:
Oh, it looks like a matter of using the API just right. You have to be explicit with the
#{label}_type
and#{label}_id
of the related models. Batman.js will load the associations from JSON just fine, but you'll have to specify them when initializing a new record, for example:I put up a JSFiddle where it's working: http://jsfiddle.net/2atLZ/2/
I'll go back to the docs and add a note about this! Long term, would be great if the API just accepted
#{label}
then extracted#{label}_id
and#{label}_type
... but for now, it's not so.!