Git Product home page Git Product logo

riotux's Introduction

riotux event controller inspired in Flux Pattern.

Intro

Simple Event Controller for Riot.js, inspired in Flux Pattern. riotux provides more organization for datastore in your application. Your Stores can call and listen events to other Stores and Components, that can call and listen events to Stores too. The Dispatcher is used to communicate between Components.

See the Demo.

Install

Requires Riot 2.0+

Npm:

npm install riotux

Bower:

bower install riotux

Manual install

Just include dist/riotux.min.js file in your project.

Examples:

Blog example with Riot.js and riotux

Stores:

The Stores are a riot.observable(). Stores can listen and trigger methods for other Stores and Components. The method will applied for the Store that you passing in argument like 'storeName'. If you have two methods with the same name, the method that will be call is the method to the store that have the same name that you passed in arguments.

Stores Data-Flow example:

// Your Car Store
function CarStore ( ) {
  riot.observable(this);
  
  // listen to 'start' event
  this.on('start', function ( person ) {
    console.log(person + ' started the Car.')
    // Emmit the method for view that listen to
    this.trigger('carMoving', person);
  });
};

var carStore = new CarStore();
riotux.addStore('carStore', carStore);
// Your Other Store
function PersonStore ( ) {
  riot.observable(this);
 
  // listen to 'startCar' event
  this.on('startCar', function ( person ) {
    riotux.trigger('carStore', 'start', person);
  });
};

var personStore = new PersonStore();
riotux.addStore('personStore', personStore);
/**----------------------------------- 
  * Data-Flow: Component -> personStore -> carStore -> Component.
  *-----------------------------------
  * When the component is mounted, trigger the event 'startCar' to 
  * personStore, passing 'You' like argument.
  * The personStore trigger the event to carStore passing the argument too.
  * carStore recieves and trigger for the components that listen to event.
  */

// In your component .tag
this.on('mount', function ( ) {
  riotux.trigger('personStore', 'startCar', 'You');
});

// listen the method from carStore
riotux.on('carStore', 'carMoving', function (person) {
  console.log(person + ' started the Car.');
});

// > output: You started the Car.

Dispatcher

The Dispatcher connects your Component with other Components. If you need to listen a method present in one Component inside your Component, you need register this using the method riotux.listen, that will register your method inside the Dispatcher. You can use the methodriotux.emmit passing the event name for the method that you want to call to other Component that listen to.

Dispatcher Data-Flow example in View:

<!-- In your .tag component -->

<script>
    var self = this; 
    self.status = false;
    
    self.on('mount', function(){
      riotux.emmit('eventName', 'Argument');  
    });
</script>
<!-- In your other .tag component -->

<script>
    var self = this; 
    self.status = false;
    
    riotux.listen('eventName', function ( arg ) {
      console.log(arg);
    });

</script>
// > output: Argument.

API

All stores should be created and registered before the Riot app is mounted to works fine.

Add an Store:

  • riotux.addStore(storeName, Store)

Stores:

  • riotux.on(storeName, event, callback): listen the event for the Store that pass like storeName.

  • riotux.trigger(storeName, event [, args]): trigger the event for the Store that pass like storeName.

  • riotux.one(storeName, event, callback): listen the event, just one time, for the Store that pass like storeName.

  • riotux.off(storeName, event [, callback]): Cancel the event for the Store that pass like storeName.

Dispatcher:

  • riotux.listen(event, callback): listen an event

  • riotux.emmit(event [, args]): trigger an event.

  • riotux.listenOne(event, callback): listen an event, just one time.

  • riotux.cancel(event [, callback]): remove an event.

Helper methods:

  • riotux.getDispatcherEvents( eventAPI ): return the events initialized in Dispatcher at the momen, eventAPI can be: listen, listenOne, emmit and cancel.

License

MIT License.

riotux's People

Contributors

luisvinicius167 avatar

Watchers

James Cloos 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.