简体   繁体   English

Ember.js事件不起作用

[英]Ember.js event not working

I wrote this very simple Ember code, but for some reason the event doesn't seem to fire when I press the button. 我写了这个非常简单的Ember代码,但是由于某些原因,当我按下按钮时,该事件似乎没有触发。 Any tips? 有小费吗?

<!DOCTYPE html>

<html>

  <head>
    <title>Test</title>
  </head>

  <body>

    <script type="text/x-handlebars">
      {{#view CounterView}}
        Counter: {{Counter.value}}

        <button {{action "increment"}}>Add 1</button>
      {{/view}}
    </script>

    <script src="jquery-1.7.2.min.js"></script>
    <script src="handlebars-1.0.rc.1.min.js"></script>
    <script src="ember-1.0.pre.min.js"></script>

    <script>
      Counter = Ember.Object.create({
        value: 1,
      })

      CounterView = Ember.View.extend({
        increment: function(event) {
          Counter.incrementProperty('value');
        }
      })
    </script>
  </body>
</html>

In order to make the event system working, Ember.js needs an application to be created and initialized (the framework does it for you). 为了使事件系统正常工作,Ember.js需要创建和初始化一个应用程序(框架为您完成了此工作)。

Applying your example to the default Ember.js starting jsfiddle, here is the result: 将您的示例应用于默认的以jsfiddle开头的Ember.js,结果如下:

Html template: HTML模板:

<script type="text/x-handlebars" data-template-name="application">
  {{#view MyApp.CounterView}}
    Counter: {{MyApp.counter.value}}
    <button {{action "increment"}}>Add 1</button>
  {{/view}}
</script>

Javascript: 使用Javascript:

//Thanks to @Trek Instructions:
//The following line creates one listener for each user event (e.g. 'click') and
//controls event delegation.
MyApp = Ember.Application.create();

MyApp.counter = Ember.Object.create({
  value: 1
});

MyApp.CounterView = Ember.View.extend({
  increment: function(event) {
    MyApp.counter.incrementProperty('value');
  }
});

​ Working fiddle: http://jsfiddle.net/6p6XJ/260/ 工作小提琴: http//jsfiddle.net/6p6XJ/260/

I would advise you to read http://trek.github.com/ , particularly the "The Smallest Viable Ember Application" part. 我建议您阅读http://trek.github.com/ ,尤其是“最小可行的Ember应用程序”部分。

Starting from 1.0.pre, Ember apps require a router to be present. 从1.0.pre开始,Ember应用程序需要存在路由器。 However, most of the documentation/examples on net refer to 0.9.5 era 但是,网上的大多数文档/示例均指的是0.9.5时代

trek guide + ember docs are a great way to start with the framework. 迷航指南+灰烬文档是从框架开始的好方法。

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

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