2

我已经FIXTURES使用 ember-model 创建了,但我无法在模板上使用以下节点

    "logged_in": {
        "id" : "1",
        "logged": true,
        "username": "sac1245",
        "account_id": "4214"
    } 

我已经belongsTo为这个节点实现了关系,但它抛出了一个错误:Uncaught Error: Ember.Adapter must implement findQuery
这里我列出了我的模型代码:

Astcart.Application =  Ember.Model.extend({
    name: Ember.attr(),
    logo_url : Ember.attr(),
    list: Ember.hasMany('Astcart.List', {key: 'list', embedded: true}),
    logged_in: Ember.belongsTo('Astcart.Logged_in', {key: 'logged_in'})
});

    Astcart.List = Ember.Model.extend({
      name: Ember.attr()
    });

    Astcart.Logged_in = Ember.Model.extend({
      id: Ember.attr(),
      logged: Ember.attr(),
      username: Ember.attr(),
      account_id: Ember.attr()
    });

    Astcart.Application.adapter = Ember.FixtureAdapter.create();

    Astcart.Application.FIXTURES = [
        {
            "id"     : "1",
            "logo_url": "img/logo.jpg",
            "logged_in": {
                "id" : "1",
                "logged": true,
                "username": "sac1245",
                "account_id": "4214"
            },          
            "name": "gau",
            "list": [
                        {
                            "id"     : "1",
                            "name": "amit"
                        },
                        {
                            "id"     : "2",                 
                            "name": "amit1"
                        }
            ]
        }
    ];

模板代码:

    {{#each item in model}}
        {{item.name}}
        {{item.logged_in.logged}}                               
    {{/each}}

路由器代码:

  Astcart.ApplicationRoute = Ember.Route.extend({
    model: function() {
      return Astcart.Application.find();
    }
  }); 

谁能告诉我如何在模板中访问上述节点的数据?

4

1 回答 1

3

您当前的夹具已logged_in嵌入数据:

Astcart.Application.FIXTURES=[
    {
        ...
        "logged_in": {
            "id": "1",
            "logged": true,
            "username": "sac1245",
            "account_id": "4214"
        },
        ...
    }
];

所以logged_in属性映射需要参数embedded: true,才能找到这个数据,如下:

Astcart.Application =  Ember.Model.extend({
  ...
  logged_in: Ember.belongsTo('Astcart.Logged_in', {key: 'logged_in', embedded: true })
});

如果没有这个,它将尝试查询Astcart.Logged_in

Astcart.Logged_in.find(1)

并且因为它没有适配器,所以会失败并出现以下异常:Uncaught Error: Ember.Adapter must implement findQuery

你可以在那个小提琴http://jsfiddle.net/marciojunior/8zjsh/中看到这个工作

于 2013-09-02T14:01:59.467 回答