Created
March 30, 2017 20:26
-
-
Save toranb/398fb54ad3a6c09c97b4cb604f7d4f84 to your computer and use it in GitHub Desktop.
New Twiddle
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'; | |
| import hbs from 'htmlbars-inline-precompile'; | |
| const createStore = (reducer) => { | |
| var pointer = 0; | |
| var state = []; | |
| var listeners = []; | |
| var firstState = reducer(); | |
| state.push(firstState); | |
| var store = { | |
| getState() { | |
| return state[pointer]; | |
| }, | |
| dispatch(action) { | |
| var nextState = reducer(state[pointer], action); | |
| pointer++; | |
| state.push(nextState); | |
| listeners.forEach((callback) => { | |
| callback(); | |
| }); | |
| }, | |
| subscribe(callback) { | |
| listeners.push(callback); | |
| }, | |
| goBack() { | |
| pointer--; | |
| listeners.forEach((callback) => { | |
| callback(); | |
| }); | |
| }, | |
| goForward() { | |
| pointer++; | |
| listeners.forEach((callback) => { | |
| callback(); | |
| }); | |
| } | |
| } | |
| return store; | |
| }; | |
| const reducer = (state = 0, action) => { | |
| if (action && action.type === 'ADD') { | |
| console.log('ADDING!'); | |
| return state + 1; | |
| } | |
| return state; | |
| }; | |
| const store = createStore(reducer); | |
| export default Ember.Component.extend({ | |
| init() { | |
| this._super(...arguments); | |
| store.subscribe(() => { | |
| this.notifyPropertyChange('number'); | |
| }); | |
| }, | |
| number: Ember.computed(function() { | |
| return store.getState(); | |
| }), | |
| actions: { | |
| add() { | |
| store.dispatch({type: 'ADD'}); | |
| }, | |
| back() { | |
| store.goBack(); | |
| }, | |
| forward() { | |
| store.goForward(); | |
| } | |
| }, | |
| layout: hbs` | |
| <h1>{{number}}</h1> | |
| <button onclick={{action "add"}}>ADD</button> | |
| <button onclick={{action "back"}}>Back</button> | |
| <button onclick={{action "forward"}}>Forward</button> | |
| ` | |
| }); |
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'; | |
| export default Ember.Controller.extend({ | |
| appName: 'Ember Twiddle' | |
| }); |
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": { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment