简体   繁体   English

控制器最佳实践emberJS

[英]Best practice emberJS for controllers

I have this structure in my routes. 我的路线上有这种结构。

this.resource('stations',  function() {
   this.route('view', {path: ':name'});
   this.route('edit', {path: ':name/edit'});
   this.route('create', {path: ':name/create'});
});

I have these controllers 我有这些控制器

App.StationsEditController = Ember.ObjectController.extend({
   actions: {
      editStation: function() {
        //Logic for edit Station
      }
   }
});

App.StationsCreateController = Ember.ObjectController.extend({
   actions: {
      createStation: function() {
        //Logic for create Station
      }
   }
});

Please can you tell me if this is good practice? 请告诉我这是否是一种好习惯?

Also I would like to just use one controller called stations with a list of my actions that all of my views within that controller can call? 我也想只使用一个名为station的控制器,并列出该控制器内我所有视图都可以调用的动作列表。 For Example: - 例如: -

App.StationsController = Ember.ObjectController.extend({
   actions: {
      createStation: function() {
        //Logic for create Station
      }
      editStation: function() {
        //Logic for create Station
      }
   }
});

Open to ideas about this scenario. 开放有关这种情况的想法。

Thank you. 谢谢。

The way you have your controllers set up is considered the norm, so yes, it's good practice. 设置控制器的方式被认为是规范,所以是的,这是一种好习惯。 :) :)

However, combining the actions in a single controller won't work like that. 但是,将动作组合到单个控制器中将无法正常工作。 Actions have a specific bubbling path. 动作具有特定的冒泡路径。 They try the route's controller first, then the route, then they bubble up the route hierarchy. 他们先尝试路线的控制器,然后再尝试路线,然后冒充路线层次。 They do not bubble up the controller hierarchy. 它们不会使控制器层次结构冒泡。 So if you wanted to put the actions together like that, you'd have to define the actions in your StationsRoute , not your StationsController . 因此,如果要像这样将动作组合在一起,则必须在StationsRoute定义动作,而不是在StationsController定义动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM