Last active
April 29, 2016 07:06
-
-
Save faceyspacey/eb9e5567a90e49cecbf7667a9df55a7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as actions from './actions'; | |
| class MyComponent extends React.Component { | |
| componentWillMount() { | |
| this.props.subscribe('posts', [categoryId, limit, page}); | |
| } | |
| render() { | |
| <View> | |
| {this.props.posts.map((post) => <View><Text>{post.title}</Text</View>)} | |
| </View> | |
| } | |
| } | |
| export default connect( | |
| ({collection}) => ({posts: collection.posts}), | |
| actions | |
| )(MyComponent) | |
| /** reducer **/ | |
| export function collections(state={}, action={}) { | |
| switch (action.type) { | |
| case types.SUBSCRIPTION_FETCING: | |
| return { | |
| ...state, | |
| [action.collection]: false, | |
| }; | |
| case types.SUBSCRIPTION_ADDED: | |
| return { | |
| ...state, | |
| [action.collection]: action.payload, | |
| }; | |
| //case types.SUBSCRIPTION_CHANGED/ETC... | |
| default: | |
| return state; | |
| } | |
| } | |
| /** action creator **/ | |
| export function subscribe(subscriptionName, params) { | |
| return (dispatch, getState) => { | |
| dispatch({type: +'SUBSCRIPTION_FETCHING', collection: subscriptionName}); | |
| return ddpClient.subscribePromise(subscriptionName, [params]) | |
| .then(() => { | |
| ddpClient.observe(subscriptionName); | |
| observer.added = function(id) { | |
| dispatch({ | |
| type: 'SUBSCRIPTION_ADDED', | |
| collection: subscriptionName, | |
| payload: ddpClient.collections[subscriptionName][id], | |
| }); | |
| }; | |
| //observer.etc | |
| }) | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment