[英]Ember.js event not working
我寫了這個非常簡單的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需要創建和初始化一個應用程序(框架為您完成了此工作)。
將您的示例應用於默認的以jsfiddle開頭的Ember.js,結果如下:
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');
}
});
工作小提琴: http : //jsfiddle.net/6p6XJ/260/
我建議您閱讀http://trek.github.com/ ,尤其是“最小可行的Ember應用程序”部分。
從1.0.pre開始,Ember應用程序需要存在路由器。 但是,網上的大多數文檔/示例均指的是0.9.5時代
迷航指南+灰燼文檔是從框架開始的好方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.