使用vuexfire动态绑定Firebase集合列表

问题描述

我在vuex商店中有一个Firebase文档列表,并且该列表会动态更新。我想使用vuexfire动态绑定该列表中的文档。

state: {
   docsToBind: [],// dynamic: this gets updated so I cannot hardcode a few bindings
   documents: {} // bind documents here?
},actions:{
   bindOne: firestoreAction(({ state,bindFirestoreRef }) => {
      return bindFirestoreRef(...)
      }),bindAll({state}){
   for(const doc of state.docsToBind){
       // bind the document to a dictionary item?
   },unbindTeam({state}){
      // whenever a doc is removed from the listunbind it
   }   
}

这是正确的方法吗?

注意:我无法绑定整个集合,因为客户端无法访问所有文档。所以我需要绑定docsToBind

中的子集

解决方法

使用Vuexfire进行绑定时,您只应提供绑定状态的键以及源(集合,查询或文档)。

因此,如果要绑定到集合的子集,则需要绑定到Query,因此,您需要知道查询定义(即子集的定义)。

如果要在“一个动作”中绑定一组文档(定义是动态的,例如,一组通过可变ID标识的文档),则可能需要使用另一种方法。

例如,您可以定义查询,方法是在文档中具有ID的字段,然后使用$arr = ['products' => [ ['LOTid' => [20200901000001],'Brand' => 'SAMSUNG','Product' => 'TV','DisplayOrder' => 6],['LOTid' => [20200901000002],'Brand' => 'LG','DisplayOrder' => 9],['LOTid' => [20200901000003],'Product' => 'MOBILE','DisplayOrder' => 1],'DisplayOrder' => 4],],'status' => 'ok' ]; usort($arr['products'],function($a,$b){ $cmp1 = $a['DisplayOrder'] <=> $b['DisplayOrder']; //rank 1 return $cmp1 ? $cmp1 : $a['Brand'] <=> $b['Brand']; //rank 2 }); 运算符在同一字段上组合多达10个相等(in)子句带有逻辑==

或者您将需要创建自己的自制绑定,例如通过在Vuex操作中使用OR