gpt4 book ai didi

javascript - 如何触发 itemController 中定义的 Emberjs 操作

转载 作者:行者123 更新时间:2023-11-28 00:56:49 25 4
gpt4 key购买 nike

有没有办法触发 itemController 中定义的操作。当我用 <button {{action 'inItem' }}> 触发操作时,它会抛出未捕获错误:没有处理事件“inItem”。。如果我使用 <button {{action 'inItem' target=item}}> 设置操作目标,它会抛出错误,Uncaught TypeError:无法读取未定义的属性“apply”

如何触发 itemController 中定义的操作。

App = Ember.Application.create({
LOG_TRANSITIONS: true
});

App.Router.map(function(){
this.resource('posts');

});

App.PostsRoute = Ember.Route.extend({
model: function(){
return [
{title: 'success'},
{title: 'winning'},
{title: 'breakthrough'}
];
}

});

App.PostsController = Ember.Array.extend({
itemController: 'post'
});
App.PostController = Ember.ObjectController.extend({
isSelected: false,

actions: {
inItem: function(){
this.set('isSelected', true);
//alert('hi');
}
}

});
<!DOCTYPE html>
<html>
<head>
<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.s3.amazonaws.com/tags/v1.0.0/ember.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script type='text/x-handlebars' id='application'>
{{#link-to 'posts'}} All posts {{/link-to}}
{{outlet}}
</script>

<script type='text/x-handlebars' data-template-name='posts'>

<br>
{{#each item in controller }}

{{#if item.isSelected}}
{{item.title}}
{{/if}}

<br>

<button {{action 'inItem' target=item}} {{bind-attr class="isSelected"}}> call itemcontroller action </button>


{{/each}}


</script>

</body>
</html>

最佳答案

您的 Controller 应该扩展ArrayController,而不是Array

App.PostsController = Ember.ArrayController.extend({
itemController: 'post'
});

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

关于javascript - 如何触发 itemController 中定义的 Emberjs 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26128810/

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