我应该如何获取此输入字段的值?
How should I get the value of this input field?
我正在 {{input value=(cents-to-dollars model.amountInCents)}}
使用子表达式。它使用自定义助手将值从美分转换为美元。我的APIreturns仙
然而在控制器save
动作中,console.log(this.get('model.amountInCents'));
returnsundefined
。我错过了什么吗?输入助手中可能 name
或 valueBinding
?
如果我删除子表达式。 console.log(this.get('model.amountInCents'));
输出正常。
// Routes
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('product', params.product_id);
}
});
// Controller
export default Ember.Controller.extend({
actions: {
save: function() {
console.log(this.get('model.amountInCents')); // returns undefined
var _this = this;
var dollars = this.get('model.amountInCents');
var amountInCents = dollars / 100;
this.get('model').set('amountInCents', amountInCents);
this.get('model').save().then(function(product){
_this.transitionToRoute('admin.products.show', product);
}, function() {
// Need this promise, so we can render errors, if any, in the form
});
return false;
},
cancel: function() {
this.transitionToRoute('products.show', this.get('model'));
}
}
});
// Template
<form {{action "save" on="submit"}}>
<p>
<label>Name:
{{input value=model.name}}
</label>
</p>
<p>
<label>Amount in cents:
{{input value=(cents-to-dollars model.amountInCents)}}
</label>
</p>
<input type="submit" value="Save"/>
<button {{action "cancel"}}>Cancel</button>
</form>
首先,(至少在版本 1.9.1 中)您提出的建议并不真正有效(参见 here - 值出现在输入字段之外)。我认为,真正的问题是您没有绑定到 属性,而是绑定到从助手返回的字符串(这不是您想要的)。
那么,你能做什么?
您可以按如下方式设置 dollars
计算 属性:
App.IndexController = Ember.ObjectController.extend({
dollars: function(key, value){
if (arguments.length > 1) {
var dollars = value;
this.set('amountInCents', parseInt(dollars) * 100);
}
return this.get('amountInCents') / 100;
}.property('model.amountInCents')
});
完整的工作示例here
我正在 {{input value=(cents-to-dollars model.amountInCents)}}
使用子表达式。它使用自定义助手将值从美分转换为美元。我的APIreturns仙
然而在控制器save
动作中,console.log(this.get('model.amountInCents'));
returnsundefined
。我错过了什么吗?输入助手中可能 name
或 valueBinding
?
如果我删除子表达式。 console.log(this.get('model.amountInCents'));
输出正常。
// Routes
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('product', params.product_id);
}
});
// Controller
export default Ember.Controller.extend({
actions: {
save: function() {
console.log(this.get('model.amountInCents')); // returns undefined
var _this = this;
var dollars = this.get('model.amountInCents');
var amountInCents = dollars / 100;
this.get('model').set('amountInCents', amountInCents);
this.get('model').save().then(function(product){
_this.transitionToRoute('admin.products.show', product);
}, function() {
// Need this promise, so we can render errors, if any, in the form
});
return false;
},
cancel: function() {
this.transitionToRoute('products.show', this.get('model'));
}
}
});
// Template
<form {{action "save" on="submit"}}>
<p>
<label>Name:
{{input value=model.name}}
</label>
</p>
<p>
<label>Amount in cents:
{{input value=(cents-to-dollars model.amountInCents)}}
</label>
</p>
<input type="submit" value="Save"/>
<button {{action "cancel"}}>Cancel</button>
</form>
首先,(至少在版本 1.9.1 中)您提出的建议并不真正有效(参见 here - 值出现在输入字段之外)。我认为,真正的问题是您没有绑定到 属性,而是绑定到从助手返回的字符串(这不是您想要的)。
那么,你能做什么?
您可以按如下方式设置 dollars
计算 属性:
App.IndexController = Ember.ObjectController.extend({
dollars: function(key, value){
if (arguments.length > 1) {
var dollars = value;
this.set('amountInCents', parseInt(dollars) * 100);
}
return this.get('amountInCents') / 100;
}.property('model.amountInCents')
});
完整的工作示例here