Skip to content

Instantly share code, notes, and snippets.

@gregturn
Created August 17, 2015 17:10
Show Gist options
  • Select an option

  • Save gregturn/b4d561651b6997659786 to your computer and use it in GitHub Desktop.

Select an option

Save gregturn/b4d561651b6997659786 to your computer and use it in GitHub Desktop.
render: function () {
var employees = this.props.employees.map(employee =>
client({method: 'GET', path: employee._links.self.href}).then(response =>
<Employee key={response.entity._links.self.href}
headers={response.headers}
employee={response.entity}
attributes={this.props.attributes}
onUpdate={this.props.onUpdate}
onDelete={this.props.onDelete}/>
)
);
console.log(employees);
var the_results = when.all(employees).then(results => {
/* I got results right here */
});
var navLinks = [];
if ("first" in this.props.links) {
navLinks.push(<button key="first" onClick={this.handleNavFirst}>&lt;&lt;</button>);
}
if ("prev" in this.props.links) {
navLinks.push(<button key="prev" onClick={this.handleNavPrev}>&lt;</button>);
}
if ("next" in this.props.links) {
navLinks.push(<button key="next" onClick={this.handleNavNext}>&gt;</button>);
}
if ("last" in this.props.links) {
navLinks.push(<button key="last" onClick={this.handleNavLast}>&gt;&gt;</button>);
}
return (
<div>
<input ref="pageSize" defaultValue={this.props.pageSize} onInput={this.handleInput}/>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Description</th>
<th></th>
</tr>
{employees}
</table>
<div>
{navLinks}
</div>
</div>
)
}
@briancavalier

Copy link
Copy Markdown

You can't expect to be able to "extract" a value synchronously from inside the promise there, since the value might only exist in the future (and there's no blocking in JS). I think the right thing to do is fetch the employees somewhere else (not in render) and cache them. Then, implement render simply to render whatever is cached, or some default view (or empty list of employees) if the employees haven't been fetched at all yet.

Does that make sense?

@gregturn

Copy link
Copy Markdown
Author

Ahh. Maybe this.setState({/* fetched data*/}) once the data is fetched would the reactive way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment