-
Notifications
You must be signed in to change notification settings - Fork 57
Added support of shouldComponentUpdate #39
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
e7d97fd
00d68b5
c5d7301
fbbe8e4
2c4287d
ddfab83
e693bd5
05ee271
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,13 @@ | |
| updateScheduler: _.identity | ||
| }; | ||
|
|
||
| var updatable = function (component){ | ||
| if (component.shouldComponentUpdate !== undefined && component.shouldComponentUpdate !== null){ | ||
| return component.shouldComponentUpdate(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| var subscribe = function(component, modelOrCollection, customChangeOptions) { | ||
| if (!modelOrCollection) { | ||
| return; | ||
|
|
@@ -33,7 +40,7 @@ | |
| var behavior = React.BackboneMixin.ConsiderAsCollection(modelOrCollection) ? collectionBehavior : modelBehavior; | ||
|
|
||
| var triggerUpdate = behavior.updateScheduler(function() { | ||
| if (component.isMounted()) { | ||
| if (component.isMounted() && updatable(component)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't you just add
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldComponentUpdate isn't always defined, for that reason i've added
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok yes makes sense - strange that React doesn't have a default implementation for shouldComponentUpdate{return true;} and having the spec override the implementation (like other lifecycle methods). But looks like most implementations on React are like this and not Backbone esq where you override default methods with your own etc. |
||
| (component.onModelChange || component.forceUpdate).call(component); | ||
| } | ||
| }); | ||
|
|
@@ -79,7 +86,7 @@ | |
|
|
||
| unsubscribe(this, modelOrCollection(this.props)); | ||
| subscribe(this, modelOrCollection(nextProps), customChangeOptions); | ||
|
|
||
| if (typeof this.componentWillChangeModel === 'function') { | ||
| this.componentWillChangeModel(); | ||
| } | ||
|
|
@@ -98,6 +105,7 @@ | |
| componentWillUnmount: function() { | ||
| // Ensure that we clean up any dangling references when the component is destroyed. | ||
| unsubscribe(this, modelOrCollection(this.props)); | ||
|
|
||
| } | ||
| }; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the undefined test really necesarry? I would expect react to make a default one if it isn't defined. Did you check this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added line note below on the same lines to call
shouldComponentUpdate()directly in the triggerUpdateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
undefinedis not really necessary, I am accustomed to check both casesundefinedandnull