Git Product home page Git Product logo

riotcontrol's Introduction

RiotControl

npm install riotcontrol

A Simplistic Central Event Controller / Dispatcher For RiotJS, Inspired By Facebook's Flux Architecture Pattern.

RiotControl is, in the spirit of Riot itself, extremely lightweight. It forgoes elements of Flux, to favor small and simple applications. RiotControl passes events from views to stores, and back, relying heavily on Riot's observerable API. Stores can talk to many views, and views can talk to many stores.

Example data flow:

  1. TodoList view triggers 'todo_remove' event to RiotControl.
  2. RiotControl passes event along to stores.
  3. TodoStore implements a 'todo_remove' event handler, talks to back-end server.
  4. TodoStore triggers 'todos_changed' event, with new data.
  5. TodoList view implements a 'todos_changed' handler, receiving new data.

This encourages loosely coupled components. Stores house application logic and domain-specific data. Views comprise the user interface. Either side can be swapped out without interfering with the other. For example, a store that saves to local storage can be easily swapped for one that saves to a back-end service instead.

Demos

TodoList

Reference demo/todostore.js and todo.tag to understand how this works.

URL Routing Example

Reference routing_demo/index.html, itemstore.js, and item-app.tag

Binary Calculator

Things People Have Built

Flux Catalog

RiotJS version of the flux-comparison catalog.

Usage

Requires Riot 2.0+

Include riotcontrol.js, or it's few lines of code, in your project.

API

Register the store in central dispatch, where store is a riot.observable(). Generally, all stores should be created and registered before the Riot app is mounted.

RiotControl.addStore(store) 

// Example, at start of application:
var todoStore = new TodoStore() // Create a store instance.
RiotControl.addStore(todoStore) // Register the store in central dispatch.

Trigger event on all stores registered in central dispatch. Essentially, a 'broadcast' version of Riot's el.trigger() API.

RiotControl.trigger(event)
RiotControl.trigger(event, arg1 ... argN)

// Example, inside Riot view (tag):
RiotControl.trigger('todo_add', { title: self.text })

Listen for event, and execute callback when it is triggered. This applies to all stores registered, so that you may receive the same event from multiple sources.

RiotControl.on(event, callback)

// Example, inside Riot view (tag):
RiotControl.on('todos_changed', function(items) {
    self.items = items
    self.update()
})

Remove event listener.

RiotControl.off(event)

RiotControl.off(event, callback)

Same as RiotControl.on(), executes once.

RiotControl.one(event, callback)

riotcontrol's People

Contributors

jimsparkman avatar icetan avatar

Watchers

haoran 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.