流星发布/订阅中的乐观UI的非响应式更新

时间:2019-01-11 16:36:34

标签: javascript meteor

我在一组用户列表上使用withTracker。用户可以通过UI对列表进行重新排序,该UI通过流星方法更新集合中的特定项目。然后,在UI已经显示正确状态的情况下,触发withTracked触发新数据,从而导致重新渲染。是否可以使withTracker不为特定更新而触发?我很好奇是否有一个简便的解决方案,而无需创建单独的集合来保存订单数据

// 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", ....

1 个答案:

答案 0 :(得分:0)

如果要防止在特定字段上发生更新,可以将其从查询中排除:

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

这可能无法达到目的,因为您可能需要在UI中使用order字段。

也许您只需要接受更新就会发生?如果您使用的是React,它应该检测到DOM中的元素没有变化,并且无论如何都要避免ui刷新