【问题标题】:Cannot load associated records from a router (ember 1.0.0 and ember-data 1.0.0-beta.1)无法从路由器加载关联记录(ember 1.0.0 和 ember-data 1.0.0-beta.1)
【发布时间】:2013-09-03 16:32:06
【问题描述】:

我有一个加载“地点”的路由器。

App.PlaceRoute = Ember.Route.extend({
        model: function(params) {
            return this.store.find('place', params.place_id);
        },
        setupController: function(controller, model) {
            this._super(controller, model);

            //The promise way ?
            var placeId = model.get('id');
            var myRecords = this.store.find('record', {place:placeId}).then(function(recs){
                    console.log("DOES IT HAPPEN ?"); //Never logged
                    this.controllerFor('records').set('content', recs);
            });

            //Or is the good way below ?
            //this.controllerFor('records').set('content', myRecords);

            //The only that works (which is not I want but displays as I would like):
            //this.controllerFor('records').set('content', App.Record.FIXTURES);

            this.controllerFor('places').set('content', this.store.find('place'));
        },
        renderTemplate: function(){
            this.render('place', {
                    controller: 'place'  
            });
            this.render('display-graph-list', {
                    into: 'place',
                    outlet: 'graphs',
                    controller: 'records'  
            });
        }
    });

这个地方有关联的“记录”。

App.Place = DS.Model.extend({
        name: attr('string')
        , desc: attr('string')
        , records: hasMany('record')
        , site: belongsTo("site")
    });

    App.Record = DS.Model.extend({
            name: attr('string'),
            type: attr('string'),
            data: attr('string'),
            belongsTo: attr('place')
    });

正如 ember-data 1.0.0 过渡指南中所述,它使用 Promise 来检索数据,但在这里,什么都没有发生。

这是一个 JSbin 来看看我的意思: http://jsbin.com/iMEdOCe/6/

当我导航到一个地方时,我应该至少显示一条记录。

我不知道如何加载和显示记录。我错过了什么?

有没有更好的方法来显示模型的子项数组?

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    这是 ember-data 1.0.0-beta.1 中两个问题的组合

    缺少显示是由于 hasMany/belongsTo 关系:将这些行注释到 App.Place 和 App.Record 中会显示某些内容。

    但是出现了另一个问题:不应使用“数据”关键字。

    将其替换为“数据路径”之类的内容即可。`

    编辑: Ember-data 1.0.0-beta.2 解决的问题

    【讨论】:

    • 我认为评论属于的作品,因为你有这个映射:belongsTo: attr('place'),而你想要的是:place: belongsTo('place')
    • 感谢您的指导。我很困惑,因为您的建议适用于我的 jsbin,但不适用于我的本地机器。我继续调试
    • 好的,我终于找到了,这很棘手:从 jsbin 复制粘贴到我的本地服务器后,我仍然有相同的行为(房间描述下方没有显示)。但是,我从 URL 中删除了 # 之后的部分,然后按回车键。然后硬重置我的页面......奇迹般的工作......直到我浏览了几次然后它就消失了。显然,这是来自 ember 的错误。每次我重置 URL 时都会发生这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2015-02-21
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多