【发布时间】:2014-02-02 14:10:38
【问题描述】:
我正试图围绕 ember.js。我正在编写一个显示某些咖啡豆价格的小型网络应用程序。用户可以添加新的咖啡豆,但现在我想让用户能够编辑现有的咖啡豆。如果用户双击 bean 的条目,那么他或她应该能够输入新名称或价格:
<script type="text/x-handlebars" data-template-name="coffee">
<table>
<tr>
<th>Beans</th>
<th>Prices</th>
</tr>
{{#each}}
<tr>
{{#if isEditing}}
<td>{{input type="text"}}</td>
<td>{{input type="text"}}</td>
<td><button class="delete">Delete</button></td>
{{else}}
<td {{action "editCoffee" on="doubleClick"}}>{{bean}}</td>
<td {{action "editCoffee" on="doubleClick"}}>{{price}}</td>
<td><button class="delete">Delete</button></td>
{{/if}}
</tr>
{{/each}}
</table>
{{input type="text" placeholder="Beans" value=newBean}}
{{input type="text" placeholder="Price" value=newPrice}}
<button type="button" {{action 'createCoffee'}}>Submit</button>
</script>
这是控制器的代码:
// Controllers
App.CoffeeController = Ember.ArrayController.extend({
actions: {
createCoffee: function() {
// Get the bean name
var bean = this.get('newBean');
if (!bean.trim()) { return; }
// Get the price
var price = this.get('newPrice');
if (!price.trim()) { return; }
// Create the new coffee model
var coffee = this.store.createRecord('coffee', {
bean: bean,
price: price
});
// Clear the text fields
this.set('newBean', '');
this.set('newPrice', '');
// Save the new model
coffee.save();
},
isEditing: false,
editCoffee: function () {
console.log('Hello World');
this.set('isEditing', true);
}
}
});
这里是 JS Fiddle 的链接:http://jsfiddle.net/cspears2002/y8MT3/
双击名称或价格确实可以让我进入 editCoffee 功能,但由于某些原因我无法编辑咖啡豆。有什么想法吗?
【问题讨论】:
标签: javascript ember.js