Skip to content

Commit fe6d8da

Browse files
author
Vlad Balin
committed
listenToProps improvements
String with prop names separated by spaces is allowed. Change events will be taken from prop values.
1 parent 246de4f commit fe6d8da

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var MyComponent = React.createClass({
155155
## Passing Backbone objects as React components props
156156
```javscript
157157
var MyComponent = React.createClass({
158-
listenToProps : {
158+
listenToProps : { // or just string with property names, separated by space
159159
model : 'change'
160160
},
161161

nestedreact.js

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nestedreact.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/createClass.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ var ListenToProps = {
2121
}
2222
};
2323

24+
var ListenToPropsArray = {
25+
componentDidMount : function(){
26+
var props = this.props,
27+
updateOn = this.listenToProps;
28+
29+
for( var i = 0; i < updateOn.length; i++ ){
30+
var emitter = props[ updateOn[ i ] ];
31+
emitter && this.listenTo( emitter, emitter.triggerWhenChanged, forceUpdate );
32+
}
33+
}
34+
};
35+
2436
var ModelState = {
2537
listenToState : 'change',
2638
model : null,
@@ -60,7 +72,16 @@ function createClass( spec ){
6072

6173
if( spec.Model ) mixins.push( ModelState );
6274

63-
if( spec.listenToProps ) mixins.unshift( ListenToProps );
75+
var listenToProps = spec.listenToProps;
76+
if( listenToProps ){
77+
if( typeof listenToProps === 'string' ){
78+
spec.listenToProps = listenToProps.split( ' ' );
79+
mixins.unshift( ListenToPropsArray );
80+
}
81+
else{
82+
mixins.unshift( ListenToProps );
83+
}
84+
}
6485

6586
mixins.push( Events );
6687

0 commit comments

Comments
 (0)