您可以通過在影響應用程序狀態(tài)的元素上啟用可點擊事件,將事件發(fā)送到視圖的控制器。
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>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將以上代碼保存在sending_events.html文件中
在瀏覽器中打開此HTML文件。
更多建議: