我正在尝试将蝙蝠侠中的多态关联设置为文档化的这里。我正在使用LocalStorage,并一直收到以下消息:
Related model undefined for polymorphic association not found.我的模型如下:
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我用来实例化所有的代码:
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()根据这个问题,这取决于名称空间,但它们在我的代码中都是相同的,所以我真的很想知道这里到底出了什么问题。
提前感谢
发布于 2014-01-27 01:28:24
呃哦..。我写了这个例子,所以如果它不起作用,我的责任是:)
可以肯定的是,您在脚本开始时调用了App1.run()吗?我上周在测试时遇到了这个问题 --必须打电话给run来加载关联!
编辑:
哦,看起来就像正确使用API的问题。您必须对相关模型的#{label}_type和#{label}_id进行显式处理。Batman.js将很好地从JSON加载这些关联,但是您必须在初始化新记录时指定它们,例如:
super_strength = new App1.Superpower
name: "Super Strength"
superpowerable_id: superman.get('id'),
superpowerable_type: 'Hero'我在它工作的地方安装了一个JSFiddle:http://jsfiddle.net/2atLZ/2/
我会回到医生那里,加一张关于这个的笔记!从长远来看,如果API接受#{label},然后提取#{label}_id和#{label}_type,那就太好了。但现在情况并非如此!
https://stackoverflow.com/questions/21368596
复制相似问题