EmberJS 视图发送事件
2018-01-04 11:04 更新
描述
您可以通过在影响应用程序状态的元素上启用可点击事件,将事件发送到视图的控制器。
语句
Ember.View.extend({ click: function(evt) { this.get('controller').send('ClickableEvent', params); } }); Ember.Controller.extend({ actions: { ClickableEvent: function(params){ //Do your logic } } });
例子
<!DOCTYPE html> <html> <head> <title>Emberjs Sending Events</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="logged"> <h1>Click to send an event</h1> {{#view App.LoginButton}} <button>Click Here</button> {{/view}} </script> <script type="text/javascript"> App=Ember.Application.create(); App.Router.map(function() { this.resource('logged'); }); App.LoginButton= Ember.View.extend({ click: function(evt) { //this will pass to the current route this.get('controller').send('turnItUp'); } }); App.Route= Ember.Route.extend({ //it calls when click event occurs events: { turnItUp: function(){ document.write("<b>Welcome to Tutorialspoint</b>"); } } }); </script> </body> </html>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将以上代码保存在sending_events.html文件中
在浏览器中打开此HTML文件。
以上内容是否对您有帮助:
更多建议: