簡體   English   中英

如何在路由之前確保流星本地收集已准備就緒

[英]How to ensure meteor local collection is ready before routing

我正在使用Meteor + AngularJS框架。

我想根據離線集合中存儲的數據來控制路由(使用ground:db),但是不能保證離線數據在路由之前已經准備好。

如果集合不是脫機的,則似乎有一些值得研究的方法,例如“ waitOn”或“ subscriptionReady”,但脫機集合不需要訂閱或發布,如何在路由或啟動應用程序之前確保它們已准備就緒? ?

關鍵源片段:

1.route.js

import { LastLogin } from '../lib/collections';

angular.module('app')
    .config(function ($stateProvider, $urlRouterProvider) {
            console.log(LastLogin.find().count());
            if(LastLogin.find().count() > 0){
                $urlRouterProvider.otherwise('main/homepage');
            }else{
                $urlRouterProvider.otherwise('login');
            }
        });

2.collections.js

export const LastLogin = new Ground.Collection('lastLogin', {connection: null});

在大多數情況下,LastLogin.find()。count()為0,很少為1,實際上lastLogin集合中有幾條記錄可以在顯示登錄頁面后正確打印出來,對於我。

我嘗試通過Tracker.autorun封閉以下代碼

Tracker.autorun(function(){
    if(LastLogin.find().count() > 0){
        $urlRouterProvider.otherwise('main/home');
    }else{
        $urlRouterProvider.otherwise('login');
    }
});

但沒有幫助。

我的最終目的是使最后一個用戶即使在脫機狀態下也自動登錄。 有更好的解決方案嗎?

最終我自己解決了。 這個想法是地面的:db有一個“已加載”事件,該事件在這里記錄: https : //github.com/GroundMeteor/db/blob/grounddb-caching-2016/EVENTS.md ,我幾天前看過這個文檔,但是沒有馬上猜出用法。

來源(app.js):

// Startup
if (Meteor.isCordova) {
    angular.element(document).on('deviceready', onReady);
}else {
    angular.element(document).ready(onReady);
}

function onReady() {
    Ground.on('loaded', function (ret) {
        if(ret.collection == 'lastLogin'){
            angular.bootstrap(document, ['app']);
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM