1.0.70 • Published 6 years ago

@drumtj/global-data v1.0.70

Weekly downloads
61
License
MIT
Repository
github
Last release
6 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

6 years ago

1.0.69

6 years ago

1.0.68

6 years ago

1.0.67

7 years ago

1.0.66

7 years ago

1.0.65

7 years ago

1.0.64

7 years ago

1.0.63

7 years ago

1.0.62

7 years ago

1.0.60

7 years ago

1.0.59

7 years ago

1.0.58

7 years ago

1.0.56

7 years ago

1.0.55

7 years ago

1.0.54

7 years ago

1.0.53

7 years ago

1.0.52

7 years ago

1.0.51

7 years ago

1.0.50

7 years ago

1.0.49

7 years ago

1.0.48

7 years ago

1.0.47

7 years ago

1.0.46

7 years ago

1.0.45

7 years ago

1.0.44

7 years ago

1.0.43

7 years ago

1.0.42

7 years ago

1.0.41

7 years ago

1.0.40

7 years ago

1.0.39

7 years ago

1.0.38

7 years ago

1.0.37

7 years ago

1.0.36

7 years ago

1.0.35

7 years ago

1.0.34

7 years ago

1.0.33

7 years ago

1.0.32

7 years ago

1.0.31

7 years ago

1.0.29

7 years ago

1.0.26

7 years ago

1.0.25

7 years ago

1.0.24

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago