Comparing version 0.30.0 to 0.31.0
@@ -18,3 +18,3 @@ import './../node_modules/todomvc-common/base.css'; | ||
const controller = Controller(Model({}), {}); | ||
const controller = Controller(Model({})); | ||
@@ -21,0 +21,0 @@ controller.modules({ |
@@ -15,3 +15,3 @@ import submitted from './signals/submitted'; | ||
}); | ||
module.signals({ | ||
@@ -18,0 +18,0 @@ submitted |
{ | ||
"name": "cerebral", | ||
"version": "0.30.0", | ||
"version": "0.31.0", | ||
"description": "A state controller with its own debugger", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
138
README.md
@@ -9,137 +9,3 @@ # Cerebral | ||
## The Cerebral Webpage is now launched | ||
You can access the webpage at [http://www.cerebraljs.com/](http://www.cerebraljs.com/). You will find all the information you need there. | ||
- [How to create a custom Cerebral VIEW package](#how-to-create-a-custom-cerebral-view-package) | ||
- [How to create a custom Cerebral MODEL package](#how-to-create-a-custom-cerebral-model-package) | ||
## How to create a custom Cerebral VIEW package | ||
**view** packages in Cerebral just uses an instantiated Cerebral controller to get state, do state changes and listen to state changes. The package you create basically just needs an instance of a Cerebral controller and you will have access to the following information. | ||
```js | ||
// The controller instantiated can be passed to the package. With React it is | ||
// done so with a wrapper component and with Angular using a provider. You have | ||
// to decide what makes sense for your view layer | ||
function myCustomViewPackage (controller) { | ||
// Get state | ||
controller.get(path); | ||
// Listen to state changes | ||
controller.on('change', function () { | ||
}); | ||
// Listen to debugger time traversal | ||
controller.on('remember', function () { | ||
}); | ||
}; | ||
``` | ||
That is basically all need to update the **view** layer. | ||
## How to create a custom Cerebral MODEL package | ||
In this example we will use Baobab. | ||
*index.js* | ||
```js | ||
var Baobab = require('baobab'); | ||
var deepmerge = require('deepmerge'); | ||
var Model = function (initialState, options) { | ||
options = options || {}; | ||
var tree = new Baobab(initialState, options); | ||
var model = function (controller) { | ||
controller.on('reset', function () { | ||
tree.set(initialState); | ||
}); | ||
controller.on('seek', function (seek, isPlaying, recording) { | ||
var newState = deepmerge(initialState, recording.initialState); | ||
tree.set(newState); | ||
}); | ||
return { | ||
tree: tree, | ||
get: function (path) { | ||
return tree.get(path); | ||
}, | ||
toJSON: function () { | ||
return tree.toJSON(); | ||
}, | ||
export: function () { | ||
return tree.serialize(); | ||
}, | ||
import: function (newState) { | ||
var newState = deepmerge(initialState, newState); | ||
tree.set(newState); | ||
}, | ||
mutators: { | ||
set: function (path, value) { | ||
tree.set(path, value); | ||
}, | ||
unset: function (path) { | ||
tree.unset(path); | ||
}, | ||
push: function (path, value) { | ||
tree.push(path, value); | ||
}, | ||
splice: function () { | ||
tree.splice.apply(tree, arguments); | ||
}, | ||
merge: function (path, value) { | ||
tree.merge(path, value); | ||
}, | ||
concat: function () { | ||
tree.apply(path, function (existingValue) { | ||
return existingValue.concat(value); | ||
}); | ||
}, | ||
pop: function (path) { | ||
tree.apply(path, function (existingValue) { | ||
existingValue.pop(); | ||
return existingValue; | ||
}); | ||
}, | ||
shift: function (path) { | ||
tree.apply(path, function (existingValue) { | ||
existingValue.shift(); | ||
return existingValue; | ||
}); | ||
}, | ||
unshift: function (path, value) { | ||
tree.unshift(path, value); | ||
} | ||
} | ||
}; | ||
}; | ||
model.tree = tree; | ||
return model; | ||
}; | ||
Model.monkey = Baobab.monkey; | ||
module.exports = Model; | ||
``` | ||
## Demos | ||
**TodoMVC**: [www.christianalfoni.com/todomvc](http://www.christianalfoni.com/todomvc) | ||
## Cerebral - The beginning | ||
Read this article introducing Cerebral: [Cerebral developer preview](http://christianalfoni.com/articles/2015_05_18_Cerebral-developer-preview) | ||
## Contributors | ||
- **Marc Macleod**: Discussions and code contributions | ||
- **Petter Stenberg Hansen**: Logo and illustrations | ||
- **Jesse Wood**: Article review | ||
Thanks guys! | ||
## Please head over to our website | ||
[http://www.cerebraljs.com/](http://www.cerebraljs.com/). You will find all the information you need there. |
@@ -6,4 +6,8 @@ var utils = require('./utils.js'); | ||
Object.keys(modules).forEach(function (key) { | ||
var path = modules[key].path; | ||
var module = {}; | ||
var module = { | ||
meta: modules[key].meta | ||
}; | ||
module.state = Object.keys(state).reduce(function (module, key) { | ||
@@ -10,0 +14,0 @@ module[key] = function () { |
@@ -56,3 +56,2 @@ var utils = require('./utils.js'); | ||
} | ||
var signals = utils.setDeep(controller.signals, moduleName, {}); | ||
var moduleExport = { | ||
@@ -72,3 +71,7 @@ name: actualName, | ||
getSignals: function () { | ||
return signals; | ||
var signals = controller.getSignals(); | ||
var path = moduleName.split('.'); | ||
return path.reduce(function (signals, key) { | ||
return signals[key]; | ||
}, signals); | ||
}, | ||
@@ -79,7 +82,8 @@ modules: registerModules.bind(null, moduleName) | ||
allModules[moduleName] = Object.keys(constructedModule || {}).reduce(function (module, key) { | ||
module[key] = constructedModule[key]; | ||
return module; | ||
}, moduleExport); | ||
moduleExport.meta = constructedModule; | ||
module.meta = constructedModule; | ||
allModules[moduleName] = moduleExport; | ||
return moduleExport; | ||
}); | ||
@@ -86,0 +90,0 @@ return allModules; |
@@ -146,2 +146,5 @@ var utils = require('./utils.js'); | ||
start: function () { | ||
if (window.__CEREBRAL_DEVTOOLS_GLOBAL_HOOK__) { | ||
window.__CEREBRAL_DEVTOOLS_GLOBAL_HOOK__.signals = controller.getSignals(); | ||
} | ||
var event = new Event('cerebral.dev.cerebralPing'); | ||
@@ -148,0 +151,0 @@ window.dispatchEvent(event); |
@@ -34,2 +34,3 @@ var Controller = require('./../src/index.js'); | ||
}); | ||
test.ok(ctrl.getModules().test); | ||
@@ -172,1 +173,45 @@ test.done(); | ||
}; | ||
exports['should expose signals added to module on the module object'] = function (test) { | ||
var ctrl = Controller(Model({})); | ||
test.expect(1); | ||
ctrl.modules({ | ||
test: function (module) { | ||
module.modules({ | ||
sub: function (module) { | ||
module.signals({ | ||
'test': [ | ||
function action (arg) { | ||
test.ok(module.getSignals().test); | ||
} | ||
] | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
ctrl.getSignals().test.sub.test.sync(); | ||
test.done(); | ||
}; | ||
exports['should expose meta information returned'] = function (test) { | ||
var ctrl = Controller(Model({})); | ||
test.expect(2); | ||
ctrl.modules({ | ||
test: function (module) { | ||
module.signals({ | ||
test: [ | ||
function action(arg) { | ||
test.equal(arg.module.meta.foo, 'bar'); | ||
} | ||
] | ||
}); | ||
return { | ||
foo: 'bar' | ||
}; | ||
} | ||
}); | ||
ctrl.getSignals().test.test.sync(); | ||
test.equal(ctrl.getModules().test.meta.foo, 'bar'); | ||
test.done(); | ||
}; |
4150
313309
11