流星铁路由器 - 如何等待(使用加载模板)一个会话.get

Meteor Ironrouter - how to waiton (with a loading template) a Session.get?

本文关键字:加载 get 会话 一个 何等待 流星 等待 路由器      更新时间:2023-09-26

我正在尝试使用一些变量(来自 Session.get())渲染一个"spotresult"模板。我花了很多时间在上面,但我没有找到答案......你能帮我吗?

https://git.geekli.st/balibou/hackaton_volant/tree/master/cerfvolantspatial

多谢!

//router.js
Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading'
});
Router.route('/', {name: 'spot'});

// 1.Normal rendering
//Router.route('/spotresult',{name: 'spotresult'});

-----------------------------------------------------------------------------

// 2. First attempt
//Router.route('/spotresult', {
//  name: 'spotresult',
//  action: function () {
//    if (this.ready())
//      this.render();
//    else
//      this.render('loading');
//  }
//});

//3. Second attempt
//var waitingSession = function() {
//  if (!this.ready()) {
//    this.render(this.loadingTemplate);
//  } else {
//    this.next();
//  }
//}
//
//Router.onBeforeAction(waitingSession, {only: 'spotresult'});

布局模板:

<template name="layout">
  <div class="container">
    {{> header}}
  </div>
  <div class="container mainyield">
    {{> yield}}
  </div>
</template>

加载模板:

<template name="loading">
  {{>spinner}}
</template>

现货模板:

<template name="spot">
  <div class="spot">
    {{#if currentUser}}       
      <p class="text-center">Bonjour, bienvenue sur votre profil</p>   
    {{else}}
      <h4 class="text-center">Bonjour, pour connaître la vitesse du vent, remplissez le champs ci-dessous !</h4>
      <form role="form" class="new-spot">
        <div class="form-group">
          <label for="email">Spot :</label>
          <input name="lieu" type="text" class="form-control" id="lieu" placeholder="Entrez un lieu">
        </div>
        <button type="submit" class="btn btn-default">Valider</button>
      </form>   
      {{#if windspeed7}}
        {{>spotresult}}
      {{/if}}  
    {{/if}} 
  </div>
</template>

我认为问题出在这里,请替换为此代码。

Template.spot.helpers({
  showSpinner:function(){
    if(Session.get("windspeed7" === ''){
      return true;
    }else{
      return false;         
    }
  }
});

并将此助手添加到另一个助手中

 {{#if showSpinner}}
   {{else}}
    {{>spinner}}
   {{/if}}

这应该有效