简体   繁体   中英

Several subscriptions for the same collection in MeteorJS

I'm looking for the best approach to achieve following task with MeteorJS.

Requirements are as follows: list with items, and side bar with selected item (not necessary from current page of the list).

在此处输入图片说明

I need to send two pieces of the same Items collection to the client:

  • items for current page of the list
  • one item for the side bar

What dance with publications/subscriptions should I perform?

This is not obvious to me how to do this correctly.

Currently I've added two publications for the list and selected item, subscribed to both of them on the client side. And as a result when I do Items.find() on the client to fill the list, it returns me list page items + selected item ("Item 5" appears on the first page).

It is OK when selected item should be on current page of the list ("Item 2"), but it is NOT OK when selected item should not be there and it's added to the current page anyway ("Item 5").

Your approach to use two publications and two subscriptions is correct. The consequence is that the collection on the client will contain the union of the data from the two pubsubs. Given that the two publications perform different queries, you need your two views on the client to execute those same queries on the local collection. As @KassymDorsel says, Session variables or reactive variables may be useful for managing state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM