Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix connect and listenermixin combo (fixes #118) #131

Merged
merged 4 commits into from
Nov 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ListenerMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var _ = require('./utils'),
/**
* A module meant to be consumed as a mixin by a React component. Supplies the methods from
* `ListenerMethods` mixin and takes care of teardown of subscriptions.
* Note that if you're using the `connect` mixin you don't need this mixin, as connect will
* import everything this mixin contains!
*/
module.exports = _.extend({

Expand Down
15 changes: 10 additions & 5 deletions src/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ var Reflux = require('../src'),
module.exports = function(listenable,key){
return {
componentDidMount: function(){
var warned = false;
for(var m in Reflux.ListenerMethods){
if (this[m] !== Reflux.ListenerMethods[m]){
if (this[m]){
throw "Can't have other property '"+m+"' when using Reflux.listenTo!";
}
this[m] = Reflux.ListenerMethods[m];
if (this[m] && typeof console && typeof console.warn === "function" && !warned ){
console.warn(
"Component using Reflux.connect already had property '"+m+"'. "+
"Either you had your own property with that name which was now overridden, "+
"or you combined connect with ListenerMixin which is unnecessary as connect "+
"will include the ListenerMixin methods automatically."
);
warned = true;
}
this[m] = Reflux.ListenerMethods[m];
}
var me = this, cb = (key === undefined ? this.setState : function(v){me.setState(_.object([key],[v]));});
this.listenTo(listenable,cb,cb);
Expand Down
11 changes: 11 additions & 0 deletions test/usingConnectMixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ describe('using the connect(...) mixin',function(){
assert.deepEqual([_.object([key],[triggerdata])],context.setState.firstCall.args);
});
});
describe("together with ListenerMixin in a React component",function(){
var store = Reflux.createStore({}),
def = {setState:function(){}},
fakecomponent = _.extend(def,Reflux.connect(store),Reflux.ListenerMethods);
it("should log a warning)",function(){
sinon.spy(console,"warn");
fakecomponent.componentDidMount();
assert(console.warn.calledOnce);
console.warn.restore();
});
});
});