0

这让我发疯了。我设置了一个简单的数据模型(使用 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] 中的线索?

我迷路了:(

更多信息:

我在 Mongoid 中使用 Padrino,使用 RABL 给我 JSON。正如我所说,我可以查询和模板出我的 Stack & Repo 记录。这是 /stacks.json 端点的 JSON 示例

{
"stacks": [
    {
        "account_id": null, 
        "id": "50c127ff6f094144ed000001", 
        "stars": [
            {
                "description": "Voice commanded servant for OSX", 
                "id": "50c128996f0941cfe8000001", 
                "name": "floere/james"
            }
        ]
    }
]
}
4

2 回答 2

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

  ...
于 2012-12-07T01:37:43.220 回答
0

我找到了一种让 JSON 中嵌入的相关对象正确加载的方法。基本上,您必须对序列化程序进行子类化,然后在其初始化程序中告诉它为关系注册一个映射。这是一个名为 Category 的模型类的示例,它具有一对多关系“resourceTypes”:

App.WOSerializer = DS.Serializer.extend({
   init: function(){
     this._super();
     this.map(App.Category, {
       resourceTypes: { embedded: 'load' }
     });
   }
});

我的解决方案在这里进一步解释。

于 2012-12-07T14:42:45.340 回答