[编辑]更新到 Ground DB v2,使代码更具可读性
我正在尝试在我的项目中使用 GroundDB,因此我的 Meteor-React Cordova 应用程序也可以离线运行。在主容器中,我有以下代码:
let fullyLoaded = new ReactiveVar(false);
let subscribed = false;
let subscribedOnce = false;
let checking = false;
export default createContainer(() => {
if(Meteor.status().connected && !subscribedOnce && !subscribed){
subscribed = true;
console.log("subscribing");
Meteor.subscribe("localization",
()=> {
localizationGrounded.keep(Localization.findNonGrounded());
console.log("everything has been loaded once.");
console.log("Localization count after subscription: " + Localization.find().fetch().length);
fullyLoaded.set(true);
subscribed = false;
subscribedOnce = true;
}
);
}
if(fullyLoaded.get()){
console.log("Localization Count: " + Localization.find().fetch().length)
}
return {
isLoggedIn: !!Meteor.userId(),
isLoading: !fullyLoaded.get() || subscribed,
};
}, Main);
如果尚未加载此代码,则应该订阅“本地化”。本地化集合实现如下,find() 和 findOne() 方法已被覆盖以调用接地 DB 的 find():
export const Localization = new Mongo.Collection('localization');
if(Meteor.isClient){
export let localizationGrounded = new Ground.Collection('localization', {
cleanupLocalData: false
});
//rename find() to findNonGrounded
Localization.findNonGrounded = Localization.find;
localizationGrounded.observeSource(Localization.findNonGrounded());
Localization.find = function(...args){
console.log("finding from ground db");
return localizationGrounded.find(...args);
};
Localization.findOne = function(...args){
console.log("finding one from ground db");
return localizationGrounded.findOne(...args);
}
}
但是,这会产生以下输出:
subscribing
everything has been loaded once
finding from ground db
Localization count after subscription: 28
finding from ground db
Localization count: 28
看起来不错,对吧?不幸的是,createContainer() 函数在此之后立即再次调用,导致
...
Localization count: 28
//Lots of "finding one from ground db" indicating the page is being localized correctly
finding from ground db
Localization Count: 0
//more "finding one from ground db", this time returning undefined
请帮我解决这个问题。提前致谢
紫杉醇