Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import * as actions from './actions';
  2.  
  3.  
  4. class MyComponent extends React.Component {
  5. componentWillMount() {
  6. this.props.subscribe('posts', [categoryId, limit, page});
  7. }
  8. render() {
  9. <View>
  10. {this.props.posts.map((post) => <View><Text>{post.title}</Text</View>)}
  11. </View>
  12. }
  13. }
  14.  
  15. export default connect(
  16. ({collection}) => ({posts: collection.posts}),
  17. actions
  18. )(MyComponent)
  19.  
  20. /** reducer **/
  21.  
  22. export function collections(state={}, action={}) {
  23. switch (action.type) {
  24. case types.SUBSCRIPTION_FETCING:
  25. return {
  26. ...state,
  27. [action.collection]: false,
  28. };
  29. case types.SUBSCRIPTION_ADDED:
  30. return {
  31. ...state,
  32. [action.collection]: action.payload,
  33. };
  34. //case types.SUBSCRIPTION_CHANGED/ETC...
  35. default:
  36. return state;
  37. }
  38. }
  39.  
  40.  
  41. /** action creator **/
  42.  
  43. export function subscribe(subscriptionName, params) {
  44. return (dispatch, getState) => {
  45. dispatch({type: +'SUBSCRIPTION_FETCHING', collection: subscriptionName});
  46.  
  47. return ddpClient.subscribePromise(subscriptionName, [params])
  48. .then(() => {
  49. ddpClient.observe(subscriptionName);
  50. observer.added = function(id) {
  51. dispatch({
  52. type: 'SUBSCRIPTION_ADDED',
  53. collection: subscriptionName,
  54. payload: ddpClient.collections[subscriptionName][id],
  55. });
  56. };
  57. //observer.etc
  58. })
  59.  
  60. };
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement