Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. //USAGE
  2. React.createClass({
  3. mixins: [
  4. ConnectModel(TaskStore, 'task', (t) => t.name == 'Ship Your Pets'), // <--pass finder function
  5. ConnectModel(TaskStore, 'task', 23) // <--OR pass id to use
  6. ]
  7. });
  8.  
  9.  
  10. // MIXIN DEFINITION
  11. export default function ConnectModel(store, key, finderFunc) {
  12. var fn = finderFunc;
  13. if(!_.isFunction(fn)) fn = (model) => model.id == finderFunc;
  14. var unsubscribe;
  15.  
  16. return {
  17. getInitialState: function() {
  18. var model = _.find(store.models, fn);
  19. var state = {};
  20. state[key] = model;
  21. return state;
  22. },
  23.  
  24. componentDidMount: function() {
  25. unsubscribe = store.listen(() => {
  26. var model = _.find(store.models, fn);
  27. if (!(model && model.isEqual(this.state[key]))) {
  28. var state = {};
  29. state[key] = model;
  30. this.setState(state);
  31. }
  32. });
  33. },
  34.  
  35. componentWillUnmount: function() {
  36. unsubscribe();
  37. }
  38. };
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement