gpt4 book ai didi

javascript - Ember.js 事件不工作

转载 作者:行者123 更新时间:2023-11-29 18:25:21 25 4
gpt4 key购买 nike

我写了这个非常简单的 Ember 代码,但由于某种原因,当我按下按钮时事件似乎没有触发。有什么建议吗?

<!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>

最佳答案

为了使事件系统正常工作,Ember.js 需要创建和初始化一个应用程序(框架会为您完成)。

将您的示例应用于默认的 Ember.js 启动 jsfiddle,结果如下:

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:

//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');
}
});

工作 fiddle :http://jsfiddle.net/6p6XJ/260/

关于javascript - Ember.js 事件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14048666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com