EmberJS 处理用户与操作的交互
处理用户与操作的交互
{{action}}帮助器使元素与Web应用程序中的组件交互。在这种情况下,动作在组件内部直接发送到Ember.Component实例。
Ember.Component.extend({
actions: {
//do the stuff
}
});
在上面的代码中, actions helper直接在Ember.Component实例中定义。
例子
<!DOCTYPE html>
<html>
<head>
<title>Emberjs Handling User Interaction with Actions</title>
<!-- CDN's-->
<script src="/attachements/w3c/handlebars.min.js"></script>
<script src="/attachements/w3c/jquery-2.1.3.min.js"></script>
<script src="/attachements/w3c/ember.min.js"></script>
<script src="/attachements/w3c/ember-template-compiler.js"></script>
<script src="/attachements/w3c/ember.debug.js"></script>
<script src="/attachements/w3c/ember-data.js"></script>
</head>
<body>
<script type="text/x-handlebars" data-template-name="index">
<h1>Click on button to toggle:</h1>
{{my-comp title="Click Me"}}
</script>
<script type="text/x-handlebars" data-template-name="components/my-comp">
<!-- calling the toggle function -->
<button {{action "toggleBody"}}>{{title}}</button>
{{#if isShowing}}
<h2>Tutorialspoint</h2>
{{/if}}
</script>
<script type="text/javascript">
App = Ember.Application.create();
App.MyCompComponent = Ember.Component.extend({
//action helper for component
actions: {
//toggling the text
toggleBody: function() {
this.toggleProperty('isShowing');
}
}
});
</script>
</body>
</html>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上述代码保存在 comp_action_handlr.html 文件中
在浏览器中打开此HTML文件。