Code Monkey home page Code Monkey logo

riotdispatch's Introduction

Usage:

Riot Tag:

<hello>
    <button onclick={ getApiStuff }>
        Get some stuff from our stuff api
    </button>

    <div class="api-stuff">
        { apiStuff }
    </div>
</hello>

var self = this;

// View the activity of the dispatcher in console
RiotDispatch.debug(true);

// Subscribe as a view. This component will receive global actions and view actions.
RiotDispatch.view.subscribe(self);

self.apiStuff = "";

self.getApiStuff = function() {
    // Create an action to be dispatched to APIs
    RiotDispatch.api.action('getApiStuff');
};

// Listens for an action which can be triggered with RiotDispatch.action (global action) 
// or RiotDispatch.view.action (view action)
self.on('apiStuffChanged', function(apiStuff) {
    self.apiStuff = apiStuff;
    self.update();
});

stuffApi.js

// Subscribe as an API. This subscriber will receive global actions and API actions.
var self = RiotDispatch.api.subscriber();

// Listens for an action which is triggered by a button's onclick handler in the view.
self.on('getApiStuff', function() {
    $.get('http://api.test.xyz/')
    .done(function(apiStuff) {
        // Create an action to be dispatched to stores. This action contains the data received from the API
        RiotDispatch.store.action('apiStuffReceived', apiStuff);
    });
});

module.exports = self;

stuffStore.js

// Subscribe as an API. This subscriber will receive global actions and store actions.
var self = RiotDispatch.store.subscriber();

var apiStuffChanged = function() {
    // Create an action to be dispatched to views. This action contains the current contents of the store.
    RiotDispatch.view.action('apiStuffChanged', self.apiStuff);
};

self.apiStuff = "Default value from store";
apiStuffChanged();

// Listens for an action that is triggered by the API and contains 
// data from an AJAX request, which we'd like to store.
self.on('apiStuffReceived', function(apiStuff) {
    self.apiStuff = "Stuff from API in store"
// Now that we have new data stored, run the action creator function 
// to let the view know that it needs to rerender.
    apiStuffChanged();
});

module.exports = self;

riotdispatch's People

Contributors

nkpz avatar jimsparkman avatar icetan avatar danny-andrews avatar blackbooth avatar davidbgk avatar creatorrr avatar r4j4h avatar vojtabiberle avatar

Watchers

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