1.0.70 • Published 4 years ago

@drumtj/global-data v1.0.70

Weekly downloads
61
License
MIT
Repository
github
Last release
4 years ago

global-data

npm version license

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/global-data@1.0.70/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

1.0.70

4 years ago

1.0.69

4 years ago

1.0.68

5 years ago

1.0.67

5 years ago

1.0.66

5 years ago

1.0.65

5 years ago

1.0.64

5 years ago

1.0.63

5 years ago

1.0.62

5 years ago

1.0.60

5 years ago

1.0.59

5 years ago

1.0.58

5 years ago

1.0.56

5 years ago

1.0.55

5 years ago

1.0.54

5 years ago

1.0.53

5 years ago

1.0.52

5 years ago

1.0.51

5 years ago

1.0.50

5 years ago

1.0.49

5 years ago

1.0.48

5 years ago

1.0.47

5 years ago

1.0.46

5 years ago

1.0.45

5 years ago

1.0.44

5 years ago

1.0.43

5 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.29

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago