0.2.3 • Published 8 years ago
datacast v0.2.3
Datacast.js
npm install datacast --save
This is a data layer framework, works like jQuery custom events. One side to obtain data by registering a receiver ( receive
method)
The other side send data to a corresponding receiver by send
methods.
Send
method by a path string to determine which receiver needs to be triggered.
Path string can have a hierarchy. For example grandfather.father.children
, through .
to split.
(it also can be a array,For example['grandfather','farther','children']
)
For the receiver, the child can receive the data from parent. For sending method, the parent can send data to all children.
E.g:
receive('test',data=>{
console.log('1:',data);
});
receive('test.2',data=>{
console.log('2:',data);
});
receive('test.2.3',data=>{
console.log('3:',data);
});
send('test','a');
send('test.2','b');
send('test.2.3','c');
/*
* output:
1: a
2: a
3: a
2: b
3: b
3: c
* */
###API
/**
* register a data receiver
* @ param {string|Array} path receive data from which path .can be a string or array (the string through the '. 'to split level)
* @ param {function} receiver Receives the data after the execution of the callback function, the callback function accepts two parameters (data: data, path: the path string)
* @ return {function} returns the receiver
*/
function receive(path, receiver)
/**
* register a data receiver, which will be triggered only once
* @ param {string|Array} path receive data from which path .can be a string or array (the string through the '. 'to split level)
* @ param {function} receiver Receives the data after the execution of the callback function, the callback function accepts two parameters (data: data, path: the path string)
* @ return {function} returns the receiver
*/
function receiveOnce(path, receiver)
/**
* cancellation data receiver
* @param {string|Array} path cancel which path,And its children.can be a string or array(the string through the '. 'to split level)
* @return {undefined}
*/
function cancel(path)
/**
* Send the message to the specified path
* @param {string|Array} path Send data to which path.
* @param data Data
* @return {undefined}
*/
function send(path, data)