Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在没有可用模型的情况下,我可以在Ember.js应用程序中转换到路由吗?_Javascript_Ember.js_Routes - Fatal编程技术网

Javascript 在没有可用模型的情况下,我可以在Ember.js应用程序中转换到路由吗?

Javascript 在没有可用模型的情况下,我可以在Ember.js应用程序中转换到路由吗?,javascript,ember.js,routes,Javascript,Ember.js,Routes,我正在Ember中实现一个日历应用程序。我的路由器是这样设置的: App.Router.map(function() { this.resource('month', { path: '/month/:year/:month' }); }); 现在,我想有按钮导航到下一个月或上一个月。在这一点上,我没有加载下一个月/上一个月的模型,我只知道我想去哪个月和年份,所以我希望能够像这样过渡: this.transitionTo('month', { year: 2014, month: 1

我正在Ember中实现一个日历应用程序。我的路由器是这样设置的:

App.Router.map(function() {
    this.resource('month', { path: '/month/:year/:month' });
});
现在,我想有按钮导航到下一个月或上一个月。在这一点上,我没有加载下一个月/上一个月的模型,我只知道我想去哪个月和年份,所以我希望能够像这样过渡:

this.transitionTo('month', { year: 2014, month: 1 });
但是,Ember希望我的参数是一个模型,将跳过Route#model hook on month Route并直接将提供的对象设置为Route#setupController中的控制器


我认为我的场景是相当普遍的,从我的谷歌上看,它似乎曾经被支持,但它只是不适合我。请告诉我,当我只知道所涉及的模型的ID,而无法访问实际的模型实例时,如何转换到路由。

由于您的路由是一个复合密钥,因此您必须发送一个对象。Ember将对象视为路线的模型。如果您只是发送一个非对象,或者一个
id
Ember,那么它就会点击模型钩子并执行您期望的操作

因此,您必须在setupController中处理它,以下是总体思路

this.transitionTo('month', { year: 2014, month: 1, needsGetting: true });

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return ['red', 'yellow', 'blue'];
  },
  setupControler: function(controller, model){
    if(Em.get(model, needsGetting)){
       // model = get the model
    }
    this._super(controller, model);
  }
});
正如我在评论中提到的,您也可以使用模型挂钩

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return ['red', 'yellow', 'blue'];
  },
  setupControler: function(controller, model){
    if(Em.get(model, needsGetting)){
       //model = this.model(model);
       // model = get the model
    }
    this._super(controller, model);
  }
});
或者你可以取下一个模型

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return ['red', 'yellow', 'blue'];
  },
  actions : {
    previous: function(){

    },
    next: function(){
      var model = this.get('currentModel');
      //figure out next hash
      var nextModel = this.model(nextModelParams);
      this.transitionTo('month', nextModel);
    }     
  }
});

看来你是对的。然而,我认为这应该是一个bug/特性请求,因为在我的场景中,通过模型钩子加载模型是一个合法的用例。我绝对不需要在任何地方重复我的模型加载逻辑。谢谢这绝对不是一个bug,可能是一个低优先级的特性。您的情况创建了这样一个场景:Ember本身无法知道模型和存根模型之间的区别。此外,您不需要重复模型获取逻辑,您可以使用
this.model(params)
setupController
调用它。此外,由于您的转换是在代码中进行的,因此您可以在代码中获取模型,并使用实际模型(甚至是对模型的承诺)的
transitiono