comfyvars
v1.1.3
Published
Super-Easy Observable (and Debuggable!) Values in Javascript! It's like push notifications for your variables.
Downloads
6
Maintainers
Readme
ComfyVars
Super-Easy Observable (and Debuggable!) Values in Javascript! It's like push notifications for your variables. ComfyVars lets you add a callback event to be notified whenever a value is changed in your object.
var normalObject = {}; // no callback
var comfyObject = ComfyVars.Watchable( ( prop, value ) => {} ); // YES CALLBACK!
Instafluff
Come and hang out with us at the Comfiest Corner on Twitch!
https://twitch.tv/instafluff
https://twitter.com/instafluffTV
Instructions
Node
- Install
comfyvars
npm install comfyvars --save
- Create watchable objects using ComfyVars
var ComfyVars = require("comfyvars");
var example = ComfyVars.Watchable( ( prop, value ) => {
console.log( "Watching:", prop, value );
} );
example.name = "Hello World";
// This will output: "Watching: name Hello World"
Browser
- Add
comfyvars.js
<script src="comfyvars.min.js"></script>
- Create watchable objects using ComfyVars
<html>
<head>
<script src="comfyvars.min.js"></script>
</head>
<body>
<script type="text/javascript">
var example = ComfyVars.Watchable( ( prop, value ) => {
console.log( "Watching:", prop, value );
} );
example.name = "Hello World";
// This will output: "Watching: name Hello World"
</script>
</body>
</html>
Example
var player = ComfyVars.Watchable( ( prop, value ) => {
if( prop === "xp" ) {
checkForLevelUp();
}
} );
player.name = "Instafluff";
player.xp = 0;
function checkForLevelUp() {
if( player.xp > 5 ) {
console.log( "Level up!" );
}
}
player.xp += 5;
player.xp += 5; // Console: Level up!
Debug Values
You can also debug values easily by creating a Debuggable
:
var debug = ComfyVars.Debuggable();
debug.test = "Debug World";
var testGet = debug[ "test" ];
/* This will output:
DEBUG: test = Debug World
*/
Initial Value
Values can be initialized by passing in the object as a parameter:
var debug = ComfyVars.Debuggable( { test: "Debug World" });
var testGet = debug[ "test" ];
var player = ComfyVars.Watchable( ( prop, value ) => {
if( prop === "xp" ) {
checkForLevelUp();
}
}, { name: "Instafluff", xp: 0 } );
function checkForLevelUp() {
if( player.xp > 5 ) {
console.log( "Level up!" );
}
}
player.xp += 10;