简体   繁体   中英

Non reactive update for optimistic UI in Meteor pub/sub

I'm using withTracker on a collection of user lists. The user has the ability of reordering the lists through the UI which updates the specific item in the collection through the meteor method. This then triggers withTracked to fire with the new data causing a re-render in a case where the UI is already showing the correct state. Is it possible to make withTracker not fire for particular updates? I'm curious if there is an easly solution without creating separate collections to hold the order data

// Server
Meteor.publish('lists', function (userId) {
  return Lists.find({ userId: Meteor.userId() });
});

...


Meteor.methods({
  "lists.setorder"({ listId, order}) {
     return Lists.update(listId, { $set: { order } })
  }
})
// Client
...

export default withTracker(props => {
    const listsHandle = Meteor.subscribe("lists");
    const lists = Lists.find({}).fetch();
    return { 
       ready: listsHandle.ready(),  
       lists 
    }
})(ListView)

...

// After the lists are re-ordered in the UI I call this to update the state in the backend:
Meteor.call("lists.setorder", ....

If you want to prevent an update happening on a particular field, you can exclude it from your query:

const lists = Lists.find({},{fields: {order: 0}}).fetch();

This may defeat the purpose because you probably need the order field in your UI.

Maybe you just need to accept that the update will happen? If you are using React, it should detect that there is no change to the elements in the DOM, and avoid the ui refresh anyway

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