admin管理员组

文章数量:1391991

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?

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?

Share Improve this question asked May 5, 2015 at 21:04 Pablo JomerPablo Jomer 10.4k11 gold badges59 silver badges104 bronze badges 1
  • You can also look at third-party libraries like EventEmitter2 or any other pub-sub style lib. – Michelle Tilley Commented May 5, 2015 at 22:35
Add a ment  | 

2 Answers 2

Reset to default 8

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)

Well if you are using the flux implementation given by Facebook (https://github./facebook/flux), you can actually extends their FluxStore class which es 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
    }
}

本文标签: javascriptBuilding FluxReact application without NodeJs EventEmitterStack Overflow