Code Monkey home page Code Monkey logo

js-common's Introduction

Common Module

Build Status

This module provides basic services, that are handy in most applications.

Installation

npm install --save @wildebeest/common

Requirements

It's usefull to know these libraries:

  • inversify
  • @wildebeest/js-modules

Emitter

Event emitter can be global, or specific for an instance / element. Emitter is used o comunicate internal changes. basicli it "screms" when something happens, and other parts of application can react to taht. You should use EmitterService for creating new emitrer instances.

Subscribing

To listen for a change you have to subscribe to emitter. That can be done with on method.

foo(emitterService: EmitterService)
{
    let emitter: Emitter = emitterService.createEmitter();
    let subscriber: Subscriber = emitter.on('eventName', (data: any) => {
        console.log("Do Something);
    });
}

First parameter of Emitter is the event name and second parameter is a callback with data parameters, that will be called, when event is emitted. You can palce any logic to this callback. on method returns Subscriber that should be used to unsubscribe this listener.

Emitting

If you need to emit an event, you have to call emit method and specify event name and event data. Data object will be passed as a parameter to all callbacks that listen to this event.

foo(emitterService: EmitterService)
{
    let emitter: Emitter = emitterService.createEmitter();
    emitter.emit('eventName', {
        oldValue: 0,
        newValue: 2
    });
}

Unsubscribing

You should always unsubscribe event, otherwise it can lead to memory leaks and your application halting from callback overflow. The easiest way to unsubscribe, is to use Subscriber instance returned by on method.

foo(emitterService: EmitterService)
{
    let emitter: Emitter = emitterService.createEmitter();
    this.subscriber: Subscriber = emitter.on('eventName', (data: any) => {
        console.log("Do Something");
    });
}

onDestroy()
{
    this.subscriber.unsubscribe();
}

Other way to unsubscribe is to use emiter off method that accepts as a parameter Subscriber instance

EmitterService | Injectable

This service provide a way to create new Emitters and also has a global events. You can call on, emit and off method, as if you are using emitter, but this will keep its listener for the entire duration of the application. Unless you unsubscribe, of course.

DomService | Injectable

To make DOM manipulation easier, we created DomService. If helps you create HTMLElement from string and insert elements into another element.

Creating HTMLElement From String

Use DomService create method, that accepts string as a parameter and returns HTMLElement.

foo(domService: DomService)
{
    let element: HTMLElement = domService.create('<div class="header"></div>);
}

You can also create nested elements.

foo(domService: DomService)
{
    let element: HTMLElement = domService.create('<div class="header"><div class="header-item>one</div><div class="header-item">two</div></div>');
}

Make sure that string contains only one root element.

Inserting Elements Into Elements

Use DomService insert method to insert elements inside another. First parameter is array of elements to be inserted and second parameter is element that will recieve these new elements.

foo(domService: DomService)
{
    let element: HTMLElement = domService.create('<div class="header"></div>');
    domService.insert([element], document.body);
}

In this example we insert header to document body

ViewportService | Injectable

Gain access to viewport specifications like width and height. This service also emits change event when viewport changes size, for example on resize.

To access viewport width use getWidth() and to access viewport height use getHeight().

js-common's People

Contributors

pipan avatar

Watchers

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.