Skip to content

Instantly share code, notes, and snippets.

@jbailey4
Last active December 16, 2015 03:46
Show Gist options
  • Save jbailey4/d93ec1579789d6d8b4bf to your computer and use it in GitHub Desktop.
Save jbailey4/d93ec1579789d6d8b4bf to your computer and use it in GitHub Desktop.
Ember: Dynamically create/update props using only handlebar helpers
{{!--
Notice the onchange event binding
1. the get helper grabs the prop to change
2. the mut helper provides an interface to update that prop
3. the action helper packages up that interface into a callable function, which will update the prop
4. the callable function is invoked when the input changes and is passed the input's current value as the first argument, which is then used to set the prop
a. we obtain the input's current value by destructing the native Event object which is passed as an argument to the action helper, value="currentTarget.value" basically means Event.currentTarget.value
--}}
<div>
{{#each-in model as |key|}}
<input
type="number"
value={{get currentValues key}}
onchange={{action (mut (get currentValues key)) value="currentTarget.value"}}>
{{/each-in}}
</div>
import Ember from 'ember';
const {
Route
} = Ember;
export default Route.extend({
model() {
return {
foo: 1,
bar: 2,
baz: 3
}
}
});
import Ember from 'ember';
const {
Controller
} = Ember;
export default Controller.extend({
currentValues: Ember.Object.create({})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment