Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function (window) {
- function Store() {
- this.state = {};// estado de la aplicacion
- this.reducers = {};// acciones para actualizar el estado sin mutarlo
- this.subscribers = {};// componentes atentos a cambios
- }
- Store.prototype.setSubscriber = function (name, subscriber) {
- this.subscribers[name] = subscriber;
- };
- Store.prototype.setReducer = function (name, reducer, initState = {}) {
- this.state[name] = initState;
- this.reducers[name] = reducer;
- };
- Store.prototype.dispatch = function (action) {
- var newState = {};
- var reducerNames = Object.keys(this.reducers);
- reducerNames.forEach(function (reducerName) {
- var currentState = this.state[reducerName];//puede ser del local storage
- newState = this.reducers[reducerName](currentState, action);
- this.state[reducerName] = $.extend({}, currentState, newState);
- });
- var subscriberNames = Object.keys(this.subscribers);
- subscriberNames.forEach(function (subscriberName) {
- var updateViewFn = this.subscribers[subscriberName];
- if (updateViewFn) {
- updateViewFn(this.state, action);
- }
- });
- };
- window.Store = Store;
- })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement