createRuleStr: function (selector, rule) { var props = ''; each(rule, function (value, key) { props += key + ':' + value + ';'; }); return selector + '{' + props + '}'; },
update: function (state) { if (state === this.lastAppState) { return; } var dynamicCss = this.entities.getAllComponentsOfType('css'); each(dynamicCss, this.updateDynamicCss, this, [state]); this.lastAppState = state; },
update: function (currentAppState) { if (currentAppState === this.lastState) { return; } var stateComponents = this.entities.getAllComponentsOfType('globalToLocal'); each(stateComponents, this.updateEntity, this, [currentAppState]); this.lastState = currentAppState; },
prepare: function (raw, result, selector) { each(raw, function (value, key) { if (value && typeof value === 'object') { this.prepare(value, result, this.combineSelector(selector, key)); return; } result[selector] = result[selector] || {}; result[selector][key] = value; }, this); return result; },
updateEntity: function (globalToLocal, entityId, appState) { var newState = this.entities.getComponentData(entityId, 'state') || {}; if (utils.isFunction(globalToLocal)) { newState = globalToLocal(appState, newState); } else { each(globalToLocal, function (localKey, globalPath) { newState[localKey] = immutable.find(appState, globalPath); }); } this.entities.setComponent(entityId, 'state', newState); }
}).whenBrewed(function () { this.state = immutable.fromJS({ route: '#/', todos: [] }); this.ui = UI.brew({ messages: this.messages, }); each([ StorageController.brew(), TodoController.brew(), ], this.wireUp, this); });
wireUp: function (controller) { if (!controller) { throw 'Invalid input: Empty value'; } if (!controller.messages) { throw 'Invalid input: Message map missing'; } each(controller.messages, function (fnName, message) { this.messages.on(message, function (data) { var fn = controller[fnName]; this.state = fn.call(controller, this.state, data); }, this); }, this); },
renderer: function (ctx) { var h = ctx.h; var todos = ctx.state.val('todos'); var hasTodos = todos.length > 0; var allCompleted = ctx.state.val('allCompleted'); return h('.main', { className: hasTodos ? '' : 'hidden' }, [ h('input.toggle-all', { type: 'checkbox', checked: allCompleted, }), h('label', { htmlFor: 'toggle-all' }, 'Mark all as complete'), h('ul.todo-list', null, each(todos, ctx.placeholder, ctx)) ]); },
setRules: function (rules) { each(this.prepare(rules, {}, ''), this.setRule, this); },