2016-09-23 44 views
0

我正在使用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()計數( )爲0,很少會是1,事實上lastLogin集合中有幾條記錄可以在登錄頁面顯示後正確打印出來,這對我來說已經太遲了。

我試圖通過Tracker.autorun

包圍下面的代碼
Tracker.autorun(function(){ 
    if(LastLogin.find().count() > 0){ 
     $urlRouterProvider.otherwise('main/home'); 
    }else{ 
     $urlRouterProvider.otherwise('login'); 
    } 
}); 

,但沒有幫助。

我的最終目的是讓上次用戶自動登錄,即使在離線狀態下也是如此。有更好的解決方案

回答

0

最後我自己完成了。這個想法是基礎: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']); 
     } 
    }); 
}