我的Ember应用程序中有以下内容:
模板:
...
<tr id="court-tpl">
<td class="name">
<input class="court-name" type="text" size="3" maxlength="3">
</td>
<td class="placement">
<select class="court-placement">
<option selected="selected" value="0">Exterior</option>
<option value="1">Indoor</option>
</select>
</td>
<td class="material">
<select class="court-material">
<option value="0">Muro</option>
<option selected="selected" value="1">Cristal</option>
</select>
</td>
<td class="single">
<select class="court-single">
<option value="0" selected="selected">No</option>
<option value="1">Sí</option>
</select>
</td>
<td>
<button {{action "deleteCourtRow" target="view" on="click"}} class="btn btn-small"><i class="icon-remove"></i> Eliminar</button>
</td>
</tr>
...
对应观点:
var ClubCourtsView = Ember.View.extend({
...
deleteCourtRow: function(event) {
console.log(event);
}
...
});
单击该按钮时,控制台输出为undefined
。我正在尝试获取event.target以删除包含内联表单的<tr>
元素。
知道发生了什么事吗?
修改
控制器:
var ClubCourtsController = Ember.Controller.extend({
needs: ['currentClub'],
content: null,
addingCourts: false,
userCanAddCourts: function() {
if (this.get('addingCourts') || this.get('hasCustomCourts'))
return true;
return false;
}.observes('addingCourts', 'hasCustomCourts'),
hasCustomCourts: function() {
if (this.content.get('customCourts').get('length') > 0)
return true;
return false;
}.property('customCourts'),
saveCourts: function(data) {
// Scope reference
var that = this;
// Create a record for each row
$.each(data, function(i) {
var elId = data[0].elId;
var r = App.CustomCourt.createRecord();
r.set('name', data[0].name);
r.set('single', data[0].single);
r.set('crystal', data[0].crystal);
r.set('indoor', data[0].indoor);
r.set('club', that.content);
// Remove this row form when model is saved,
// as the table is watching for new courts automatically
r.one('didCreate', function() {
$('#' + elId).fadeOut('fast');
});
});
// Send new courts to back-end and update clubmeta: enable custom courts
this.content.get('clubmeta').set('customCourtsEnabled', true);
this.store.commit();
},
deleteCourt: function(court) {
court.deleteRecord();
court.get('transaction').commit();
}
});
module.exports = ClubCourtsController;
问题在于表格有两种行:显示现有记录(法院)的行和显示创建表格的表格。有一个按钮可以动态添加更多行,因为俱乐部可以有很多法院。
现有行上的删除按钮从数据库中删除CustomCourt
记录,该记录在控制器的deleteCourt()
中完成。
删除表单行上的按钮(问题所在的位置)应该只删除视图中的HTML元素,这就是我使用视图函数deleteCourtRow()
的原因。只有在按下保存按钮时才会创建这些行的模型(请参阅saveCourts()
中的循环)。
http://i.stack.imgur.com/nMj4l.png
无论如何,请随意推荐另一种方法,因为我是使用Ember的完全新手。