1.0.70 • Published 5 years ago

@drumtj/global-data v1.0.70

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

5 years ago

1.0.69

5 years ago

1.0.68

6 years ago

1.0.67

6 years ago

1.0.66

6 years ago

1.0.65

6 years ago

1.0.64

6 years ago

1.0.63

6 years ago

1.0.62

6 years ago

1.0.60

6 years ago

1.0.59

6 years ago

1.0.58

6 years ago

1.0.56

6 years ago

1.0.55

6 years ago

1.0.54

6 years ago

1.0.53

6 years ago

1.0.52

6 years ago

1.0.51

6 years ago

1.0.50

6 years ago

1.0.49

6 years ago

1.0.48

6 years ago

1.0.47

6 years ago

1.0.46

6 years ago

1.0.45

6 years ago

1.0.44

6 years ago

1.0.43

6 years ago

1.0.42

6 years ago

1.0.41

6 years ago

1.0.40

6 years ago

1.0.39

6 years ago

1.0.38

6 years ago

1.0.37

6 years ago

1.0.36

6 years ago

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.32

6 years ago

1.0.31

6 years ago

1.0.29

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago