可以使用action
把手幫手,見http://jsfiddle.net/jmDXP/
把手:
<script type="text/x-handlebars" id="mainview" >
{{#each list}}
<div {{action "clicked"}} >{{title}}/{{desc}}</div>
{{/each}}
</script>
<script type="text/x-handlebars" >
<div id="container"></div>
{{#view App.TabView}}
{{view Ember.TextField valueBinding="title"}}
{{view Ember.TextField valueBinding="desc"}}
<button {{action "addTab" }} >Add</button>
{{/view}}
</script>
的JavaScript:
var get = Ember.get;
App = Ember.Application.create({});
App.Tab = Ember.Object.extend({
title: null,
desc: null
});
App.TabsController = Ember.ArrayController.create({
content: [],
resourceType: App.Tab,
addTab: function(title, desc) {
this.pushObject(App.Tab.create({
title: title,
desc: desc
}));
},
init: function() {
this._super();
var i = 0;
for (i = 0; i <= 4; i++) {
this.addTab("hola" + i, "test " + i);
}
}
});
App.TabView = Ember.View.extend({
addTab: function() {
var title = get(this, 'title');
var desc = get(this, 'desc');
App.TabsController.addTab(title, desc);
}
});
App.initializeView = Ember.View.create({
templateName: 'mainview',
listBinding: 'App.TabsController',
clicked: function(view, event, context) {
console.log('clicked @', get(context, 'title'));
}
});
App.initializeView.appendTo('#container');
注:在即將到來的版本的action
回調改變的參數,見emberjs action event.target is undefined
我補充說: 「jQuery的」 標籤到這個問題,因爲它是JQuery的有關。 – 2012-03-14 08:50:47
@MikeAski如果你給編輯的建議,這將是很好的保持他們的實質和改善其他事情,如格式化拼寫等**也**在帖子:) – gideon 2012-03-14 09:43:20
thnks @MikeAski我是一個stackoverflow新手 – 2012-03-14 12:08:17