最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Building FluxReact application without NodeJs EventEmitter - Stack Overflow

programmeradmin1浏览0评论

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
    }
}
发布评论

评论列表(0)

  1. 暂无评论