简体   繁体   English

ember.js中的事件处理程序

[英]Event Handlers in ember.js

I am trying to trigger an event from an Ember.js controller. 我正在尝试从Ember.js控制器触发事件​​。 I throws an error saying "ember.min.js:3 Uncaught Error: Nothing handled the action 'clicked'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.". 我抛出一个错误,指出“ ember.min.js:3未捕获的错误:什么都没有处理动作'单击'。如果您确实处理了该动作,则可能是由于从控制器中的动作处理程序返回true导致此错误而导致的泡沫。” Also I am not clear on how the event system works on itself for the targeted action. 我也不清楚事件系统如何针对目标动作自行工作。 C an someone help me with this. 有人可以帮我弄这个吗。 Here is my code: 这是我的代码:

<!DOCTYPE html>
<html>
   <head>
      <title>Ember.js Application example</title>
      <!-- CDN's -->

      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>
      <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="http://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
       <script src="http://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
   </head>
   <body>
      <div class="container" id="github-app">

      </div>       
      <script type="text/x-handlebars" data-template-name="application">
         <div class="row">
            <div class="col-md-12">
                <h1>Hello from Ember!</h1>
                {{outlet}}
            </div>
         </div>      
      </script>

      <script type="text/x-handlebars" data-template-name="index">
      <p>This is a github explorer for all the users.</p>

      <ul>{{#each dev in controller}}
         <li><a href="#">{{dev}}</a></li>
         {{/each}}
      </ul>

      <p>
         <button class="btn btn-success" {{action "clicked"}}>Click Me</button>
      </p>

      <p>{{renderedOn}}</p>
      </script>      

      <script type="text/javascript">
      App = Ember.Application.create({
         rootElement:"#github-app"
      });
      App.IndexRoute=Ember.Route.extend({
         model:function(){
            return [
            "Sam",
            "Sandy",
            "Samudh"
            ];
         }
      });
      App.IndexController = Ember.ArrayController.extend({
         renderedOn : function(){
            return new Date();
         }.property(),
         actions    : {
            clickMe(){
               alert("I been clicked");
            }            
         }

      });
      </script>
   </body>
</html>

Also available at http://jsbin.com/cuxajiyuqa/edit?html,output 也可以从http://jsbin.com/cuxajiyuqa/edit?html获得,输出

You don't have an action with the name 'clicked', and you tell your code to look for an action 'clicked' on clicking the button. 您没有名称为“ clicked”的操作,并且告诉您的代码在单击按钮时查找“ clicked”的操作。 Change 更改

{{action "clicked"}} 

into 进入

{{action "clickMe"}}

or change the 或更改

clickMe(){ 

action into 行动成

clicked(){ 

and everything should be fine. 一切都会好起来的。

Check https://guides.emberjs.com/v2.4.0/templates/actions/ for the documentation on actions 查看https://guides.emberjs.com/v2.4.0/templates/actions/获取有关操作的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM