这让我疯了。我有一个简单的数据模型设置(使用Padrino);我已经过了实际收到任何错误消息的阶段,但是将'App.Repo'模型添加到'App.Stack'模型只是......不起作用。
App.Store = DS.Store.extend({
revision: 10
adapter: DS.RESTAdapter.create({
bulkCommits: false,
mappings: {
stars: App.Stars,
stacks: App.Stacks
}
})
});
App.Stack = DS.Model.extend({
url: DS.attr('string'),
repos: DS.hasMany('App.Repo')
});
App.Repo = DS.Model.extend({
name: DS.attr('string'),
url: DS.attr('string'),
description: DS.attr('string'),
language: DS.attr('string'),
watchers: DS.attr('number'),
stack: DS.belongsTo('App.Stack'),
stackId: DS.attr('number')
});
var store = App.get('router.store');
newStack = store.createRecord(App.Stack);
console.log(newStack.serialize())
-> Object {url: null} // no mention of a repos array like I was expecting?
newStack.set('url', 'http://google.com');
console.log(newStack.serialize());
-> Object {url: "http://google.com"} // this works though
var repo = App.Repo.find().objectAt(0);
console.log(repo.serialize());
-> Object {name: "floere/james", url: "https://github.com/floere/james", description: "Voice commanded servant for OSX", language: "Ruby", watchers: 97…}
// so this exists too…
repos = newStack.get('repos');
repos.pushObject(repo);
newStack.get('repos.length'); // 1 (repos.toArray() etc etc all work too)
// but then…
console.log(newStack.serialize())
-> Object {url: null}
// and so then I try to save the relationship on the server anyway…
store.commit()
=> {"stack"=>{"url"=>nil}} // in my Ruby server logos
商店设置得很好,与我的后端交谈(例如向/repo.json提交POST发送正确的请求);它只是不承认App.Stack有任何关系。
不知道出了什么问题或者寻求帮助:(
我尝试在Ruby控制台中创建关系,然后在视图中访问它们。这就是发生的事情
// in the router
router.get('applicationController').connectOutlet('body', 'stacks', router.get('store').findAll(App.Stack));
// in the view
<script type="text/x-handlebars" data-template-name="stacks">
{{#each stack in controller }}
{{stack.id}} // this works
{{stack.url}} // this works
{{stack.repos.length}} // this returns the correct count
{{#each repo in stack.repos}}
// this loops the right number of times. so there *is* something there. somehow.
{{repo}} // prints out <App.Repo:ember490>
{{repo.id}} // prints out [object Object]
{{/each}}
{{/each}}
关于最后一点 - 可能是[object Object]中的线索?
我迷路了:(
我正在使用Padrino和Mongoid,使用RABL给我JSON。正如我所说,我可以查询&amp;模板出我的Stack&amp;回购记录。这是/stacks.json端点的JSON示例
{
"stacks": [
{
"account_id": null,
"id": "50c127ff6f094144ed000001",
"stars": [
{
"description": "Voice commanded servant for OSX",
"id": "50c128996f0941cfe8000001",
"name": "floere/james"
}
]
}
]
}
答案 0 :(得分:0)
我认为您必须通过循环遍历repos
数组手动将hasMany关系添加到您的json对象。我在我的适配器的createRecord
方法中执行此操作。
createRecord: (store, type, record) ->
data = {}
data[root] = @toData(record, { includeId: true })
repos = []
stack.get("repos").forEach (repo) ->
repos.pushObject repo.serialize()
data[root]["repos"] = repos
...
答案 1 :(得分:0)
我找到了一种方法来在JSON中获取嵌入的相关对象以正确加载。基本上你必须继承序列化器,然后在它的初始化器中,你告诉它注册关系的映射。下面是一个名为Category的模型类的示例,它具有多对多关系'resourceTypes':
App.WOSerializer = DS.Serializer.extend({
init: function(){
this._super();
this.map(App.Category, {
resourceTypes: { embedded: 'load' }
});
}
});
我的解决方案进一步解释为here。