Git Product home page Git Product logo

vento's Introduction

Vento

Vento is a event library that automatically creates event methods. Vento is used to be the super class of a class. You can also simply extend your class with Vento. After this you'll be able to either fire the event from the class itself or from external. You are also able to register to the event internally AND externally. All callbacks will be executed.

I implemented this library because I could not find another library doing this simple stuff. I wrote this code nearly in every project in my company or in private projects, so I outsourced the code from the projects.

The whole source is written in ES2015, so the examples are also written in ES2015 syntax.

Install

npm install vento

Usage

First you have to import Vento to the file you want to use it.

import Vento from 'vento';

Then extend your class with Vento.

class MyClass extends Vento {}

After this you can register new events either in the constructor or somewhere else.

this.addEvent('eventName');

You can also bind an internal method directly to your event.

this.addEvent('eventName', this.eventName);

Then you can register to the event. You can create an internal on function or register from external to your event.

eventName() {
  // do something
}

or

myClass.on('eventName', () => {
  // do something
});

or

myClass.onEventName(() => {
  // do something
});

Thats it... now you can fire your custom created event.

myClass.fireEventName();

or

myClass.fireEventName('some', { data: true });

Full example

import Vento from 'vento';

class MyClass extends Vento {
  constructor() {
    this.addEvent('test', this.test);
  }

  test(data1, data2) {
    console.log('inner notification', data1, data2);
  }
}

const myClass = new MyClass();
myClass.on('test', (data1, data2) => {
  console.log('outter notification bound with on', data1, data2);
});

myClass.onTest((data1, data2) => {
  console.log('outter notification bound with onTest', data1, data2);
});

myClass.fireOnTest('first object', 'second object');

Extend extended class

what? ...

If you have a class that already extends a class... or is extended by a class you should also have the ability to use Vento. So you can just hook Vento in the constructor of your sub class.

class SubClass extends SuperClass {
  constructor() {
    super();
    Vento.extend(this);

    this.addEvent('test');
  }
}

Copyright and license

Code and documentation released under the MIT license.

vento's People

Contributors

dweidenfeld avatar

Watchers

 avatar  avatar

vento's Issues

Extend a subclass

Extend a Subclass. I imagine something like:

class MainClass {
  constructor() {
    console.log('main constructor');
  }
}

class SubClass extends MainClass {
  constructor() {
    super();
    Vento.extend(this);

    console.log('sub constructor');

    this.addEvent('test', () => {
      console.log('test event fired');
    });
  }
}

const inst = new SubClass();
inst.fireTest();

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.