2

My webapp allows its users to interact with items. These items can be put in the trash bin, and thus be removed from regular items. I have a dedicated sub-menu called "trash bin" where users can recycle or delete the items previously put in the trash bin.

I want to conditionally display the trash bin menu, only if there are items in the trash bin depending on the user rights.

However, I don't want to publish the items in the trash bin before the user goes to the trash bin sub-menu.

What I currently do is calling a method which returns a flag (is there or not items in the user trash bin?). But this is not reactive: if another user trash an item, the sub-menu will only appear once the page has been reloaded.

How can I publish a reactive boolean flag?

I considered using one cursor.Observe() per menu/user and notify the creation (via update) or deletion (via remove) of an item using a publication.changed() on a fake published collection with one item (with one boolean field) but it seems overkill considering the resources used for the task.

I also considered updating a dedicated field in every user profile when an item status change. Here again, this is too much operations for a simple feature: I would need to assess every user right for each operation involving an item status change, and each time a user rights change, re-assess his right to recycle every item.

The less consuming alternative would be to wrap my method call in the menu template.Autorun(). However, this is not a reactive solution since it triggers only when the menu is re-rendered.

Have you faced the same kind of issue? Do you have a possible alternative solution in mind (performance-wise)?

Community
  • 1
  • 1
Billybobbonnet
  • 3,156
  • 4
  • 23
  • 49
  • am not sure what is the Complexity. Why dont you use the reactive variable. please Follow (link) [https://atmospherejs.com/meteor/reactive-var] – gatolgaj Oct 06 '15 at 08:47
  • A reactive variable, as well as a reactive dictionary, is a client side feature. My issue here is to notify reactively to the client the existence of items in the trash bin without sending them. This is something computed on the server and served on the client depending on his user rights. Basically, I am looking for the features of a publication for a single boolean variable. – Billybobbonnet Oct 06 '15 at 08:59
  • How about using `Meteor.wrapAsync` to call a meteor method which returns count of items in trash Collection. method will contain something like `return (trash.find({}).count()===0)` – gatolgaj Oct 06 '15 at 09:16
  • I don't need a synchronous method. My async method works well, its issue is that it is not reactive (as it would be for methods wrapped in `Meteor.wrapAsync` afaik). One workaround that I didn't mention would be to wrap the function call in a `Meteor.setInterval` but it is not the kind of solution I am looking for (too much resource consuming). – Billybobbonnet Oct 06 '15 at 09:33
  • I'm probably stupid so please teach me here. What it sounds like is you want something on your users screen to pop up when anyone puts something in the trash bin. If that is true, why not add a function that sets a variable that is watched by your template? That way they know that it is on and you can run your publish if they want. – Tim Roberts Oct 06 '15 at 09:50
  • Please check this package which can give you the count reactive manner. https://github.com/percolatestudio/publish-counts – gatolgaj Oct 06 '15 at 09:53
  • @TimRoberts "when anyone puts something in the trash bin" not really, I have to check the users rights to see an item depending on its ownership. Your approach is similar to mine actually, I call my method in a `Template.menu.rendered()` function and I bind the result to a reactive variable. This reactive variable is returned by an helper to set the appropriate state in the UI (menu button displayed). The subscribtion happens only when the user click on the displayed menu button. – Billybobbonnet Oct 06 '15 at 10:04
  • @gatolgaj Thanks for the link, this is an interesting package. I will end up creating a dedicated publication with this approach but it might be a good way to go. I'm gonna give it a try and keep you posted so you can post it as an answer if it is satisfying. – Billybobbonnet Oct 06 '15 at 10:07
  • [This](http://stackoverflow.com/questions/20689113/meteor-how-to-check-if-item-in-array-field-but-exclude-that-field-in-publish) might help. – Tim Roberts Oct 06 '15 at 10:39
  • @gatolgaj This is working fine. I'm still not sure on the performances impact, but it get things done with a dedicated publication count (without populating the collection on the current page with the trashed items). You are welcome to post an answer, I will validate it. – Billybobbonnet Oct 06 '15 at 11:03
  • @Tim Roberts thanks for the link. I knew about this technique from there : http://stackoverflow.com/questions/30807390/build-a-reactive-publication-with-additional-fields-in-each-document. The publish-counts package approach finally does a similar thing (using observers) but with more sugar coating. – Billybobbonnet Oct 06 '15 at 11:04

1 Answers1

0
  1. Create a new collection, ex: TrashBinState that contains a document with the (presumably shared) state of your trash bin
  2. Update that document server-side whenever an item is trashed
  3. Publish it
  4. Have every client subscribe to that publication
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39