gpt4 book ai didi

javascript - Emberjs Controller 目标指向错误的路线

转载 作者:行者123 更新时间:2023-12-02 16:53:28 25 4
gpt4 key购买 nike

我有一个学生路线,并为该路线创建了一个学生 Controller 。在 route ,我尝试使用 this.get('target') 从路线触发一个操作,该操作由于某种未知原因返回 index 路线,它是错误的路线。因此,调用该操作会引发错误,表明没有任何内容处理该操作。这是jsfiddle

App = Ember.Application.create();

App.Router.map(function(){
this.route('student');
});

App.StudentRoute = Ember.Route.extend({

actions: {

callMe: function(){
alert('hello');
}
}
});

App.StudentController = Ember.ObjectController.extend({

actions: {
sendtoRoute: function(){

console.log('the route is:', this.get('target') );
}
}
});
<script type="text/x-handlebars">
{{render 'student'}}
{{outlet}}
</script>

<script type="text/x-handlebars" id='student'>
<button {{action 'sendtoRoute'}} > hit me </buton>
</script>

<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>

<script src="http://builds.emberjs.com/release/ember.js"></script>

<script src="http://builds.emberjs.com/beta/ember-data.js"></script>

最佳答案

实际上,controller.get('target') 指向您案例中的正确上下文。来自 Ember 的 docs on Ember.Controller.target :

It can also be set after a controller has been instantiated, for instance when using the render helper in a template, or when a controller is used as an itemController. In most cases the target property will automatically be set to the logical consumer of actions for the controller.

{{render 'student'}} 使用模板“student”创建 StudentView 并将其连接到 StudentController 的新实例,然后将结果渲染到父级的模板(在您的情况下是 index )。它不会创建/传输到 StudentRoute,因此在 IndexRoute 中客户端时没有 StudentRoute 的事件实例。因此,StudentController.target 指向父上下文 - ApplicationController

关于javascript - Emberjs Controller 目标指向错误的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26390949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com