javascript - Controller 名称 Ember JS

标签 javascript ember.js

我有这些 Controller

clusters_controller.js.coffee

Portal.DashboardClustersController = Ember.ArrayController.extend

dashboard_controller.js.coffee

Portal.DashboardController = Ember.ArrayController.extend

我有一个模板,我试图从中提交一个关于dashboardCluster Controller 中的操作的表单,但我收到以下错误

Uncaught ReferenceError: <Portal.DashboardController:ember794>#needs does not include ``dashboard_clusters``. To access the dashboard_clusters controller from <Portal.DashboardController:ember794>, <Portal.DashboardController:ember794> should have a不。

模板中的表单

<form role="form" {{action "createCluster" target="controllers.dashboard_clusters" on="submit"}}>

我在目标中提到的 Controller 名称应该是什么,以便我的调用执行操作 createClusterPortal.DashboardClustersController

最佳答案

为了从 Controller 调用另一个 Controller ,需要属性 needs ( http://emberjs.com/guides/controllers/dependencies-between-controllers/ ),在本例中,使用 needs 中的值 dashboardClusters 属性足以调用名为 DashboardClustersController 的 Controller 。 具体来说这是一个例子,

http://emberjs.jsbin.com/qesilanu/1/edit

js

App = Ember.Application.create();

App.Router.map(function() {
  this.route("dashboardClusters",{path:"dashboard-clusters"});
  this.route("dashboard");
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    this.transitionTo("dashboardClusters");
  }
});

App.DashboardController = Ember.Controller.extend({
  needs:["dashboardClusters"],
  test:function(){
    var dashboardClustersController = this.get("controllers.dashboardClusters");
      alert("From DashboardController calling DashboardClustersController:"+dashboardClustersController);
  }
});

App.DashboardClustersController = Ember.Controller.extend({
  needs:["dashboard"],
  actions:{
    testAction:function(){
      var dashboardController = this.get("controllers.dashboard");
      alert("From DashboardClustersController calling DashboardController:"+dashboardController);
      dashboardController.test();
    }
  }
});

hbs

<script type="text/x-handlebars">
    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="dashboard">
    in dashboard,<br/>
    <button {{action "testAction" target="controllers.dashboardClusters"}} >test2</button><br/>
    {{#link-to "dashboard"}}go to dashboard clusters{{/link-to}}
  </script>
  <script type="text/x-handlebars" data-template-name="dashboardClusters">
    in dashboard clusters,<br/>
    <button {{action "testAction"}} >test1</button><br/>
    {{#link-to "dashboard"}}go to dashboard{{/link-to}}
  </script>

关于javascript - Controller 名称 Ember JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22980654/

相关文章:

javascript - 在 ember.js 中从 Controller 加载数据

Ember.js:检查 View 元素是否插入到 DOM 中

javascript - 页面上没有显示 Ember 简单的虚假服务

javascript - 文档 keydown 绑定(bind)不适用于输入字段

javascript - 如何将变量和数据从PHP传递到JavaScript?

javascript - 如何在 jQuery 中使用多个组查找分隔符?

javascript - 在 javascript 中将默认日期格式更改为 d/m/Y

javascript - Ember 绑定(bind)在不需要时触发,在需要时不触发

javascript - 管理模板中定义的嵌套 View 的顺序

javascript - map 初始化后更改 Google Maps API 标记标签的颜色