【问题标题】:this.transitionToRoute doesn't work without paramsthis.transitionToRoute 在没有参数的情况下不起作用
【发布时间】:2015-12-02 19:11:33
【问题描述】:

StackOverflow 早安,

// routes
Router.map(function() {
    this.resource('myresource');
    this.resource('myresource', { path: 'myresource/:param1' });
});

// call transition from controller when switch not selected any params
// doesn't work...
this.transitionToRoute('myresource');

需要帮助,如果未在视图中选择任何参数,我想转到“domain.tld/myresource”。但这行不通:(

【问题讨论】:

    标签: javascript ember.js web-applications routes


    【解决方案1】:
    // router
    Router.map(function() {
        this.route('myresource');
        this.route('anothermyresource', { path: 'myresource/: param1' });   
    });
    
    // router extend for another my resource
    setupController: function(controller, model) {
            this.controllerFor('myresource').setProperties({isNew:false, content:model});
        },
        renderTemplate: function() {
            this.render('myresource');         
        },
        model: function (params) {
            if (params.param1 === 'paramValue') return this.store.find('resource', {param1: 0});
        }
    
    // and now work like as need
    this.transitionToRoute('myresource'); 
    

    【讨论】:

      【解决方案2】:

      你应该像这样嵌套你的路线:

      Router.map(function() {
          this.route('resources', {path: '/myresource', function() {
              this.route('resource', {path: '/myresource/:param1'});
          }); 
      });
      

      这应该会为您提供您请求的 URL 路由,而无需解决此问题,并清理您的 Route/View/Controller/Component 名称。

      【讨论】:

      • 好了,资源控制器应该怎么看?我想为 /myresource 和 /myresource/:param1 使用一个控制器——这是 /myresource 的过滤器。主控制器是资源不是资源
      • 你可以在路由上设置controllerName属性,但是如果这只是一个过滤器,最好使用查询参数而不是动态段。
      • 我知道,但我想要像 /myresource/value 这样的 url 进行过滤,因为这个 url 比 /myresource?param1=value 干净
      • 在你的选择中需要实现什么逻辑,控制器和模板应该在'resource'或'resources'中,请展示这个构造的一些例子......我是Ember的新手,并且有很多麻烦......因为互联网上的很多信息和网站上的手册已经被弃用了;)
      猜你喜欢
      • 2019-06-11
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2014-11-21
      • 2015-09-04
      相关资源
      最近更新 更多