【问题标题】:Trouble access dynamic segment in Ember.js Object Route在 Ember.js 对象路由中无法访问动态段
【发布时间】:2013-03-13 16:09:10
【问题描述】:

我在从路由访问动态段时遇到了一些奇怪的行为。

我有一个关键字模型。在显示关键字集合时,我想要读取/keywords 的路径。访问单个关键字时,我希望读取/keyword/:KEYWORD_ID 的路径。按照惯例,Ember 希望您执行以下操作……

this.resource('keywords', { path: '/keywords' }, function() {

    this.route('new', { path: '/new' });
    this.resource('keyword', { path: '/keyword/:keyword_id' }, function() {

        this.route('edit');

    });

});

为了实现上述行为,我正在做以下...

this.resource('keywords', { path: '/keywords' }, function() {

    this.route('new', { path: '/new' });

});

this.resource('keyword', { path: '/keyword/:keyword_id' }, function() {

    this.route('edit');

});

但是,当使用第二种方法时,KeywordIndex(即单个关键字对象)的路由,params 对象为空,屏幕上的内容为空白。

App.KeywordIndexRoute = Ember.Route.extend({

    model: function(params) {

            return App.Keyword.find(params.keyword_id);

    },

    renderTemplate: function(controller, model) {

        this.render({outlet: 'page'});

    }

});

有人遇到过这个问题吗?有没有更好的方法来解决这个问题?

【问题讨论】:

  • 作为后续,我在这里使用了方法描述器 (stackoverflow.com/questions/14609757/…)。但是,这适用于所有情况,除非某些人通过 URL 直接访问关键字详细信息视图(即在浏览器中键入 ../keyword/ID 并点击回车)。在这种情况下,屏幕显示第二个数据,然后消失。查看模型时,您可以看到对象 (ID) 的所有数据都已删除。奇怪。
  • 你知道通过 URL 输入你的 Route 时才执行模型钩子吗?很难说问题出在哪里,因为您没有谈论如何在路线之间转换。

标签: ember.js


【解决方案1】:

问题解决了。问题出在我们针对这个特定对象的 API 中。对于那些考虑这种路由模式的人来说,最好的方法如下......

this.resource('keywords', { path: '/keywords' }, function() {

    this.route('new', { path: '/new' });

});

this.resource('keyword', { path: '/keyword/:keyword_id' }, function() {

    this.route('edit');

});

然后您可以像这样访问控制器中的模型...

App.KeywordIndexRoute = Ember.Route.extend({

    model: function(params) {

        return this.modelFor("keyword");

    },

    renderTemplate: function(controller, model) {

        this.render({outlet: 'page'});

    }

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多