Pablo Jomer
Pablo Jomer

Reputation: 10378

Building Flux/React application without NodeJs EventEmitter

Hi I'm trying to build a Flux/React application with a go-lang back-end. I have been following a tutorial I found here. But I have a problem when building the store. In the tutorial something like this is used to create a base for the store.

var ProductStore = _.extend({}, EventEmitter.prototype, {...});

The problem I have is I do not have access to the EventEmitter library which I understand is a Nodejs lib? Is there an alternative I can use?

Upvotes: 2

Views: 2109

Answers (2)

atomrc
atomrc

Reputation: 2583

Well if you are using the flux implementation given by Facebook (https://github.com/facebook/flux), you can actually extends their FluxStore class which comes with a build in eventEmitter.

The only thing if you want to do that is you must use es6 classes (and use babel to transpile to es5).

The good thing about it is that you don't have to implement the addListener removeListener and emitChange methods, and that's very DRY :)

With this solution, your stores will end up looking like that :

var FluxStore = require("flux/utils").Store,
    thing;

class ThingStore extends FluxStore {
    getThing() { 
        return thing;
    }

    __onDispatch(payload) {
       //your code
    }
}

Upvotes: 2

Caleb
Caleb

Reputation: 9458

You can use NodeJS libraries in the browser! Take a look at browserify.

First some code:

// index.js
var EventEmitter = require("events").EventEmitter;
var ProductStore = function() {};
ProductStore.prototype = new EventEmitter;

Then you run browserify on it:

browserify index.js > bundle.js

Also worth a look is WebPack which does the same thing. (but has some additional features)

Upvotes: 8

Related Questions