【问题标题】:Finding a single record by id with ember and ember data使用 ember 和 ember 数据通过 id 查找单个记录
【发布时间】:2015-02-21 23:12:51
【问题描述】:

为什么我不能在这种情况下将Id作为参数传递:

App.Router.map(function () {
    this.route("home", {path: '/'});
});

App.HomeRoute = Ember.Route.extend({
        model: function () {
           // return this.store.find('post');//works
            return this.store.find('post',1);//does not
        },
        setupController: function (controller, model) {
            controller.set('info', model);
        }
    });

小提琴:http://jsfiddle.net/3bfsddvo/2/

参考: http://emberjs.com/guides/models/finding-records/#toc_finding-a-single-record

我知道我正在尝试从我的“家”路线执行此操作,所以我猜如果创建了一个 posts/[:id] 路线,它会工作得更好。

如何在我的主路由中只返回一个帖子?

【问题讨论】:

    标签: ember.js ember-data handlebars.js


    【解决方案1】:

    我将您的 jsfiddle 移至 jsbin 并解决了错误(调试 jsbin 更容易)。我能够在控制台中看到这个错误,这是一个很好的起点:

    Error while processing route: home Assertion Failed: Cannot delegate set('info', <App.Post:ember353:1>) to the 'content' property of object proxy <App.HomeController:ember393>: its 'content' is undefined. Error: Assertion Failed: Cannot delegate set('info', <App.Post:ember353:1>) to the 'content' property of object proxy <App.HomeController:ember393>: its 'content' is undefined.
    

    老实说,我不能 100% 确定为什么会这样,但我通过进行这些更改让您的示例正常工作。

    首先我为HomeController添加了一个显式控制器:

    App.HomeController = Ember.Controller.extend({
    
    }); 
    

    然后,在您的模板中,您无需迭代任何内容,因此您可以直接显示模型:

    <ul>
      <li>{{info.id}} - {{info.title}}</li>
    </ul>
    

    JSBin

    【讨论】:

    • 好吧,是的,这是有道理的。在我发现的大多数教程和文档中,我找不到任何必须使用控制器的迹象。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2013-09-06
    相关资源
    最近更新 更多