GithubHelp home page GithubHelp logo

moduledispatcher's Introduction

Module Dispatcher

Class for frontend Modules initialization from the DOM without inline scripts. Calls Modules init() method

Installation

Install npm package

npm i module-dispatcher --save

Usage

Import it in your JavaScript file

import ModuleDispatcher from 'module-dispatcher';

Create an instance of Dispatcher

If your JavaScript Modules are parts of one global Library object, like

  • YourLibrary.moduleOne
  • YourLibrary.moduleTwo
  • YourLibrary.moduleThree

You can instantiate Dispatcher by the following way

new moduleDispatcher({
    Library : YourLibrary
});

If you don't specify Library, your Modules will be called as window.moduleOne by default.

Add Modules to the DOM

Add attribute data-module="yourModuleName" to the HTML Element of the Module you want to init.

<div data-module="comments">
    <!-- Any stuff -->
</div>

You can init multiple Modules on one node as well

<div data-module="comments likes"></div>

Passing settings to the Modules

If your Module has settings, place them via JSON inside the Node with data-module.

❗️Important

You should escape module settings data because textarea will try to process HTML special chars.

For &lt; you will get < in the textarea. That is not good.

You need to escape this string so & will be &amp;. This way &lt; will be &amp;lt; in raw HTML. Textarea will show and return &lt;.

Don't forget to add an attribute hidden to the <textarea> tag:

<div data-module="comments">
    <textarea name="module-settings" hidden>
         {
             // your module's settings ESCAPED by smth like PHP's htmlspecialchars()
         }
    </textarea>
    <!-- Other stuff -->
</div>

For several Modules on one node, your settings should be an Array

<div data-module="module1 module2">
    <textarea name="module-settings" hidden>
        [
            {
                // Module 1 settings
            },
            {
                // Module 2 settings
            },
            ...
        ]
    </textarea>
</div>

Issues and improvements

Ask a question or report a bug on the create issue page.

Know how to improve ModuleDispatcher? Fork it and send pull request.

You can also drop a few lines to CodeX Team's email.

License

MIT

moduledispatcher's People

Contributors

polinashneider avatar talyguryn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

moduledispatcher's Issues

Как подключить к Vue

Как использовать Dispatcher с Vue?

Когда подключаю к Vue, то в панели разработчика только статус "ready"

Снимок экрана 2019-04-24 в 5 14 58

Как правильно подключить и как подключить vue компоненты, чтобы они появились в панели как подключенные?

Спасибо.

Dispatcher improvement

Keep module instances on memory in SinglePageApplications. Dispatcher needs destroyer to clear out initialized modules and update or init new ones.

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.