2016-09-23 17 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']); 
     } 
    }); 
}