简体   繁体   English

如何在路由之前确保流星本地收集已准备就绪

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

I am using Meteor + AngularJS framework. 我正在使用Meteor + AngularJS框架。

I want to control routing according to what data stored in an offline collection(using ground:db), but the offline data is not guaranteed to be ready before routing. 我想根据离线集合中存储的数据来控制路由(使用ground:db),但是不能保证离线数据在路由之前已经准备好。

If the collection is not offline, there seemed to be some ways like "waitOn" or "subscriptionReady" worth researching, but offline collections need no subscriptions or publications, how can I ensure them to be ready, before routing or before the app is bootstrapped? 如果集合不是脱机的,则似乎有一些值得研究的方法,例如“ waitOn”或“ subscriptionReady”,但脱机集合不需要订阅或发布,如何在路由或启动应用程序之前确保它们已准备就绪? ?

Key source fragments: 关键源片段:

1.route.js 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 2.collections.js

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

In most of the cases, LastLogin.find().count() is 0, seldom will it be 1, in fact there are several records in lastLogin collection which can be correctly printed out after login page showed, that will be too late for me. 在大多数情况下,LastLogin.find()。count()为0,很少为1,实际上lastLogin集合中有几条记录可以在显示登录页面后正确打印出来,对于我。

I tried enclosing the following code by Tracker.autorun 我尝试通过Tracker.autorun封闭以下代码

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

but no help. 但没有帮助。

My final purpose is to get last user auto logged on, even in offline state. 我的最终目的是使最后一个用户即使在脱机状态下也自动登录。 Any better solutions? 有更好的解决方案吗?

Finally I worked it out by myself. 最终我自己解决了。 The idea is ground:db has a "loaded" event which is documented here: https://github.com/GroundMeteor/db/blob/grounddb-caching-2016/EVENTS.md , I saw this doc days ago but didn't guess out the usage immediately. 这个想法是地面的:db有一个“已加载”事件,该事件在这里记录: https : //github.com/GroundMeteor/db/blob/grounddb-caching-2016/EVENTS.md ,我几天前看过这个文档,但是没有马上猜出用法。

The source(app.js): 来源(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