Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- })
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement