Skip to content

Instantly share code, notes, and snippets.

@jgwhite
Last active March 3, 2016 05:44
Show Gist options
  • Save jgwhite/aa0efa238baa1122d541 to your computer and use it in GitHub Desktop.
Save jgwhite/aa0efa238baa1122d541 to your computer and use it in GitHub Desktop.
Ancient Redux
import Ember from 'ember';
const { inject } = Ember;
export default Ember.Controller.extend({
store: inject.service(),
patch(key, value) {
let tokens = key.split('.');
let last = tokens.pop();
let patch = {};
let cursor = patch;
tokens.forEach(t => cursor = cursor[t] = {});
cursor[last] = value;
this.get('store').dispatch({ type: 'PATCH', patch });
}
});
<pre>user = {
profile: {
name: "{{store.state.user.profile.name}}",
age: {{store.state.user.profile.age}}
}
}</pre>
<label>
Name:
<input value={{store.state.user.profile.name}}
oninput={{action (action patch "user.profile.name") value="target.value"}}>
</label>
<br>
<label>
Age:
<input value={{store.state.user.profile.age}}
oninput={{action (action patch "user.profile.age") value="target.value"}}>
</label>
import Ember from 'ember';
const { createStore } = Redux;
export default Ember.Service.extend({
init() {
this._super(...arguments);
this.store = createStore((s, a) => this.reduce(s, a));
this.store.subscribe(() => this.pull());
this.pull();
},
reduce(state = I({}), { type, patch }) {
switch(type) {
case 'PATCH':
return state.patch(patch);
default:
return state;
}
},
dispatch(...args) {
this.store.dispatch(...args);
},
pull() {
this.set('state', this.store.getState().dump());
}
});
{
"version": "0.6.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.3.1/ember.debug.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js",
"redux": "https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.js",
"ancient-oak": "https://rawgit.com/brainshave/ancient-oak/master/dist/ancient-oak-0.3.5.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment