npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

define-state

1.1.0 • Public • Published

define state Build Status Code Climate Deps

Define stateful property on class instance or any object. Behaves like defineProperty, but defines a state.

$ npm install define-state

var defineState = require('define-state');
 
function MyComponent (el) {
    this.el = el;
 
    //define state on instance
    defineState(this, 'state', this.state);
 
    //go to initial state
    this.state = true;
}
 
//state declaration
MyComponent.prototype.state = {
    true: {
        before: function () {
            this.el.innerHTML = 'Hello world!';
        }
    }
}
 
//create instance
new MyComponent(document.querySelector('#hello'));

API

defineState(target, name, state, isFn?)

Define stateful property name on a target according to the state declaration (see st8 API). Pass optional isFn argument to define functional accessor instead of getter/setter, might be useful for IE8/ES3 environments or to define functional-style components, like dialog.

defineState(target, 'visible', {
    true: function () {
        this.removeAttribute('hidden');
    },
    false: function () {
        this.setAttribute('hidden', true);
    }
}, true);
 
target.visible(false);

NPM

install

npm i define-state

Downloadsweekly downloads

6,619

version

1.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability