Created
March 30, 2017 12:26
-
-
Save toranb/0afccbea02b7472ea102d2818829a038 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains 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); | |
state.push(nextState); | |
pointer++; | |
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('state!', state); | |
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 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 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