我有这些控制器
clusters_controller.js.coffee
Portal.DashboardClustersController = Ember.ArrayController.extend
dashboard_controller.js.coffee
Portal.DashboardController = Ember.ArrayController.extend
我有一个模板,我试图在dashboardCluster控制器中提交一个表单,但我收到以下错误
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
n ...... o。
模板中的表格
<form role="form" {{action "createCluster" target="controllers.dashboard_clusters" on="submit"}}>
我在目标中提到的控制器名称应该是什么,以便我的电话转到createCluster
Portal.DashboardClustersController
答案 0 :(得分:1)
为了从控制器调用另一个控制器,需要属性needs
(http://emberjs.com/guides/controllers/dependencies-between-controllers/),在这种情况下,使用dashboardClusters
属性中的值needs
将是足以调用名为DashboardClustersController
的控制器。
具体来说,这是一个例子,
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>