Skip to content

Instantly share code, notes, and snippets.

@theptrk
Created September 8, 2015 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/ea3ff8f754faf3bc6b04 to your computer and use it in GitHub Desktop.
Save theptrk/ea3ff8f754faf3bc6b04 to your computer and use it in GitHub Desktop.
Flux with promises
import Alt from 'alt';
const alt = new Alt();
export default alt;
import KaleActions from './KaleActions';
import React from 'react';
export default class KaleForm extends React.Component {
render() {
return (
<form onSubmit={this.submitFn.bind(this)}>
<input type='text' name='kale-dish'/>
<input type='text' name='kale-dish-review'/>
<input type='submit' value='Submit'/>
</form>
);
}
submitFn(data) {
return KaleActions.create(data);
}
}
import alt from '../libs/alt';
class KaleActions {
create(data) {
return new Promise((resolve, reject) => {
var post = $.ajax({
type: 'POST',
data: data,
url: '/kaleapi',
});
post.done((postResponse) => {
resolve(postResponse);
// dispatch eventually to the store
this.dispatch(postResponse);
});
post.fail((postError) => {
reject(postError);
});
});
}
}
export default alt.createActions(KaleActions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment