Created
May 6, 2017 17:42
-
-
Save davidpatters0n/8c555abcd52b1161c1150f85e3aa05e2 to your computer and use it in GitHub Desktop.
Redux + Ember basic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}) | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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