@drumtj/global-data
v1.0.70
Published
data share between class and data watch
Downloads
72
Maintainers
Readme
global-data
data share between class and observe data
Features
- Make Object from domain string
- Supports Property watching
Installing
Using npm:
$ npm install @drumtj/global-data
Using cdn:
<script src="https://unpkg.com/@drumtj/[email protected]/dist/global-data.var.js"></script>
Using amd, commonjS Module
const GD = require('@drumtj/global-data');
import GD from '@drumtj/global-data';
static methods
GD.create(domain:String):Object
GD.set(domain:String, value:any):any
GD.get(domain:String):any
GD.watch(domainOrObj:String|Object, watchPropertyName:String, callback:(newValue:any, oldValue:any):void):Object
GD.clear()
GD.clearCallback()
GD.toJSON(domain:String):String
GD.toObject(domain:String):Object
GD.addSomeChangeListener(callback:(obj, key, newValue, oldValue):void)
GD.removeSomeChangeListener(callback)
Example
Creating a data structure
GD.create("editor.stage.options");
// same
//GD.set("editor.stage.options", {});
Set data
var timeline = {
bpm: 120
}
GD.set("editor.timeline", timeline);
Get data
GD.get(); //root
GD.get("editor");
GD.get("editor.stage");
Watch data
If you pass a domain string as the first argument, it uses the variable set in the domain, and creates a new variable if it does not exist.
// monitoring when setting values
GD.watch("editor.stage.options", 'x', function(newValue, oldValue){
//something do it
console.error("x", oldValue, newValue);
})
GD.watch("editor.timeline", "bpm", function(newValue, oldValue){
//something do it
console.error("bpm", oldValue, newValue);
})
var editor = GD.get("editor");
editor.stage.options.x = 2;
editor.timeline.bpm = 120;
//same
GD.set("editor.stage.options.x", 2);
GD.set("editor.timeline.bpm", 120);
// you can also use externally declared variables.
var foo = {};
GD.watch(foo, "bar", function(newValue, oldValue){
console.log("setted bar:", oldValue, newValue);
})
foo.bar = 10; // => output 'setted bar: undefined 10'
Clear data and watch callback
GD.clear();
Clear only watch callback
GD.clearCallback();
add a listener to call when some property changes.
function onChanged(obj, key, newValue, oldValue){
//
}
GD.addSomeChangeListener(onChanged);
GD.removeSomeChangeListener(onChanged);
get json
GD.toJSON("editor.stage");
GD.toJSON("editor.timeline");
GD.toJSON("editor");
get clone object
GD.toObject("editor.stage");
GD.toObject("editor.timeline");
GD.toObject("editor");
examples (source)
License
MIT