(function (Arsenal) {
Stablity: DEPRECATED
CAVEAT: This mechanism is deprecated, and will be removed at version 2.0.0
Requires: Binding, Trigger
A data-driven mechanism base on Binding and Trigger mechanisms.
(function (Arsenal) {
Constructor
```var impulse = new Arsenal.Impulse()*
function Impulse(data) {
this._binding = new Arsenal.Binding(data);
}
impulse.on(condition, func)
Add a new watcher with conditions
Parameters:
condition
string|object|Array Conditions for triggering the function
string Path to a single data-node
Array Pathes to multi data-nodes (checked by OR
logic)
object A condition map triggered by data-node changes, consists with one of the following types:
data-node
=> condition-function
The conditon value is a function
The condition functions will be invoked by each data-node, and the return value (true/false) will be treated as the condition result of the map entry
data-node
=> condition-value
The conditon value is a non-function value
The condition value will be checked if it equals to the data-node value and take the comparison result (true/false) as the condition result
func
function The function to be triggered (when all the conditions passed)
function addWatcher(condition, func) {
if (! Arsenal.isFunction(func)) {
return false;
}
if (Arsenal.isArray(condition)) {
var binding = this._binding,
handler = function () {
var values = [];
for (var i = 0; i < condition.length; i++) {
values.push(binding.get(condition[i]));
}
func.apply(null, values);
};
for (var i = 0; i < condition.length; i++) {
binding.bind(condition[i], handler);
}
} else if (Arsenal.isHash(condition)) {
var binding = this._binding,
trigger = new Arsenal.Trigger(),
bridge = function (value, key, modifier, former, path) {
if (Arsenal.isFunction(condition[path])) {
trigger.set(path, condition[path](value));
} else {
trigger.set(path, condition[path] === value);
}
},
handler = function () {
var values = [];
for (var i in condition) {
values.push(binding.get(i));
}
func.apply(null, values);
};
trigger.hook(handler);
for (var i in condition) {
trigger.watch(i);
binding.bind(i, bridge);
}
} else {
return this._binding.bind(condition, func);
}
}
impulse.get(path, value)
Get value by path
Parameters:
path
string Path to the data-node
Returns:
ANY Current node value
function getData(path) {
return this._binding.get(path);
}
impulse.set(path, value)
Set value by path
Parameters:
path
string Path to the data-node
value
ANY Node value
modifier
Optional, see updateSource()
function in the Binding mechanism
Returns:
ANY Old value before updating
function setData(path, value, modifier) {
return this._binding.update(path, value, modifier);
}
Primary functions
Impulse.prototype.addWatcher = addWatcher;
Impulse.prototype.getData = getData;
Impulse.prototype.setData = setData;
Accessibility functions
Impulse.prototype.on = addWatcher;
Impulse.prototype.get = getData;
Impulse.prototype.set = setData;
/* Export the Impulse mechanism */
Arsenal.Impulse = Impulse;
return Impulse;
})(this);