我有这些 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 名称应该是什么,以便我的调用执行操作 createCluster
在Portal.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/