backbone to react. what it says about awesome ui code
TRANSCRIPT
data = _.extend({ isHighlighted: this.isHighlighted }, this.user.toJSON(), this.account.toJSON() });
html = Mustache.render(Template, data);
this.$el.html(template); return this.$el;
var classes = "user-wrapper"
if (this.state.isHighlighted) { var classes += " is-highlighted"}
return ( <div className={classes}> <strong>{this.props.userName}</strong> <img src={this.props.avatar} /> <span>account: {this.props.accountName} </div>);
emails = this.user.get(‘emails')
data = { hasEmail: emails.length > 0}
html = Mustache.render(Template, data)
propTypes: {firstName: React.PropTypes.string.isRequired,emails: React.PropTypes.arrayOf([React.PropTypes.string
]),permission: React.PropTypes.oneOf(["admin","limited","normal"
])}
PropTypes: http://bit.ly/1FDQmuq& Model: http://bit.ly/1PsG9mf
if (!Array.isArray(instances)) {
type = typeof instances;msg = "listenToEvents expects array,not:";
console.warn(msg + " " + type); }
“our powers to visualize processes evolving in time are relatively poorly developed
Edsger W. Dijkstra http://bit.ly/1fzrqcm
Data
1. WillRecieveProps 2. ShouldUpdate 3. WillUpdate 4. Render 5. DidUpdate
component 1:
component 2:
Data
1. WillRecieveProps 2. ShouldUpdate 3. WillUpdate 4. Render 5. DidUpdate
Data
lifecycle methodscomponent 1:
lifecycle methods
lifecycle methods
lifecycle methods
lifecycle methods
component 2:
component 3:
component 4:
component 5:
initialize: function(model) { this.model = model this.listenTo( this.model, "change:name", this.changeName )}
changeName: function() { name = this.model.get('name')
this.$el.find('.name').html(name)}
initialize: function(model) { this.model = model this.listenTo( this.model, "change:name", this.changeName )}
changeName: function() { name = this.model.get('name')
this.$el.find('.name').html(name)}
1. Defend against future mistakes 2. Make the model of time simpler 3. Standardise the effects of time
predictability:
“"A highly composable system provides ... components that can be selected and assembled in various combinations to satisfy specific user requirements"
blog.kissmetrics.com/loading-time/
render: function() {
this.$el.html(Mustache.render(Template));
$wrapper = this.$el.find(‘.childView');
this.childView.setElement($wrapper); this.childView.render(); return this.$el;
}
Backbone: Add a child view?
Backbone: Extend a class?
SelectableRow = Row.extend({ events: function() { oldEvents = Row.prototype.events; newEvents = {'click': 'select'}; return _.extend(oldEvents, newEvents}); }
template: require('selectable-row.html'),
getViewData: function() { return model.toJSON(); }
});
toggleOpen: function() { this.setState({open: !this.state.open})}, render: function() { return ( <DropdownControls
toggleOpenState={this.toggleOpen}open={this.state.open}
> <div>shown when state.open</div> </DropdownControls>
)}
React: Add a child component
1. Fewer regressions 2. Less serious regressions 3. More code re-use 4. Shipping faster5. Better usability & UX