Skip to content

Instantly share code, notes, and snippets.

@davidpatters0n
Created May 6, 2017 17:42
Show Gist options
  • Save davidpatters0n/8c555abcd52b1161c1150f85e3aa05e2 to your computer and use it in GitHub Desktop.
Save davidpatters0n/8c555abcd52b1161c1150f85e3aa05e2 to your computer and use it in GitHub Desktop.
Redux + Ember basic
import Ember from 'ember';
const createStore = (reducer) => {
let state = reducer();
const listeners = [];
const store = {
getState() {
return state;
},
dispatch(action) {
state = reducer(state, action);
listeners.forEach((callback) => {
callback();
})
},
subscribe(callback) {
listeners.push(callback)
}
}
return store;
}
const reducer = (state, action) => {
if (action && action.type == 'INCREMENT') {
const number = state.number;
const addedNumber = number + 1;
console.log(addedNumber);
return Object.assign({}, state, {number: addedNumber});
}
return state || {id: 1, number: 1}
}
const store = createStore(reducer);
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
this._super(...arguments);
store.subscribe(() => {
this.notifyPropertyChange('number')
});
},
number: Ember.computed(function(){
return store.getState().number;
}),
actions: {
increment() {
store.dispatch({type: 'INCREMENT'})
}
}
});
<h1>Welcome to {{appName}}</h1>
<button {{action 'increment'}}>Increment</button>
{{number}}
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment