Git Product home page Git Product logo

nodeproto's Introduction

NodeProto Build Status Dependency Status devDependency Status Code Climate Test Coverage

A prototypical class that makes it easy to create Components with nodes with a powerfull instance configuration system and a react-like API.

Features

  • Clean and simple react-like syntax (states, props, method names)
  • ES6 Class support
  • Support for propTypes(required / optional / type of prop) which will be passed in while initiating a new instance.
  • 3-Way prop injection
  • Via the arguments array while creating a new instance.
  • The dataset of the element if one was passed to the instance.
  • Or the described getDefaultProps() method of your class.
  • If the props are accessed via the elements dataset, they will automatically be converted into the specified propType type(String, Number and even Objects via JSON.parse())
  • Simple on, off event observer pattern
  • ~ 400 bytes minified
  • No dependencies

Install

With npm, use the familiar syntax e.g.:

npm install nodeproto --save

once the nodeProto package is installed, just require it in your class/application file.

const nodeProto = require("nodeproto");

This package also supports AMD/RequireJS, it is defined as nodeProto. Aren't using AMD/CommonJS? Just grab a release, include the Dist/NodeProto.min.js and access the loader via the following global:

const nodeProto = window.nodeProto;

Example

// MyComponent.js
const myComponentPropTypes = {
  'myProp'              : nodeProto.propTypes.isRequired,
  'myPropNumber'        : nodeProto.propTypes.isNumber.isRequired,
  'myOptionalObjectProp': nodeProto.propTypes.isObject.isOptional
}

class MyComponent extends nodeProto.Component {
  constructor(el, props) {
    super(el, {
        'props': props,
        'propTypes': myComponentPropTypes
    });

    this.on('logSomething', this.doSomething.bind(this));
  }

  doSomething() {
    console.log(this.getProp('myProp'));
  }
}

 export default MyComponent;
import MyComponent from 'MyComponent.js';

// Create a new instance, and optionally pass in props.
const targetElement = document.querySelectorAll('[data-myComponent]')[0];
const instance = new MyComponent(targetElement, {
    'myProp': 'myString',
    'myPropNumber': 2
});

instance.trigger('logSomething') // LOG: 'myString'

PropType validators

The PropType validators are the key feature of the NodeProto. They validate your props, and optionally transform the values into the requested types by the validator.

F.e. if your component expects a Number as a prop with the name myProp, and you can't pass in the prop directly via JS, you can create a dataset property with the given name on the target element with a Number as the value e.g. data-myProp="2". Once a instance was created with this element, you can access the prop value via the getProp() method.

propTypes.isRequired

Will expect that the given key is present in either the passed props Object, the elements dataset or in the getDefaultProps() method.

propTypes.isOptional

Will log an info message into the UA's console if the given key is not present in either the passed Props-Object, the elements dataset or in the getDefaultProps() method.

propTypes.isNumber.isRequired

Like the basic propTypes.isRequired validator, but it also expects that the value is either a Number, or a String containing a Number.

propTypes.isNumber.isOptional

Like the basic propTypes.isOptional validator, but will also expect a Number as the value.

propTypes.isObject.isRequired

Like the basic propTypes.isRequired validator, but it also expects that the value is either a valid Object, or a String containing a JSON Object.

propTypes.isObject.isOptional

Like the basic propTypes.isOptional validator, but will also expect a Object as the value.

Methods

instance.getElement();

Type: Function

Will retrieve the element on which the component was mounted upon, if no element was specified, a virtual DOM element will be created.

instance.getProp(key);

Type: Function Argument key: String

Will retrieve the prop for the given key.

instance.hasProp(key);

Type: Function Argument key: String

Will return a boolean regarding the existence of the prop for the given key.

instance.getDefaultProps();

Type: Function

Should return an object with all default props you want to set.

instance.setState(key, val);

Type: Function Argument key: String Argument val: *

Will set the given state of the component.

instance.getState(key);

Type: Function Argument key: String

Will return the given state of the component.

instance.getInitialStates();

Type: Function

Should return an object with all initial states you want to set.

instance.on(eventName, listener);

Type: Function Argument eventName: String Argument listener: Function

Creates/Uses an event and attaches a callback to the given eventName.

instance.trigger(eventName, [arg1, arg2, ...]);

Type: Function Argument eventName: String Argument args: *

Triggers an event and executes all functions for the given eventName with the passed arguments.

instance.off(eventName, listener);

Type: Function Argument eventName: String Argument listener: Function

Removes the given listener from the event queue.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style.

License

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

nodeproto's People

Contributors

inkdpixels avatar

Watchers

André König avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.