vendredi 27 février 2015

How should I get the value of this input field?

I am using a subexpression at {{input value=(cents-to-dollars model.amountInCents)}}. It is using a custom helper to convert the value from cents to dollars. My API returns cents.


However in the controllers save action, console.log(this.get('model.amountInCents')); returns undefined. Am I missing something? Maybe name or valueBinding in the input helper?


If I remove the subexpression. console.log(this.get('model.amountInCents')); outputs fine.



// 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>




Aucun commentaire:

Enregistrer un commentaire