流星ReactiveVar不触发模板更新

时间:2019-04-12 00:01:36

标签: node.js meteor meteor-blaze

我有一个简单的应用程序,我试图在该应用程序上使我的数据源具有反应性,我有此集合

/imports/api/TestsCollection.js

import { Mongo } from 'meteor/mongo';
import { Factory } from 'meteor/dburles:factory';
import SimpleSchema from 'simpl-schema';


class TestsCollection extends Mongo.Collection {
    constructor(collectionName){
        super(collectionName);
        console.log('created');
    }
    insert(doc, callback) {
        const ourDoc = doc;
        ourDoc.testField = new Buffer([0,1,2]);
        const result = super.insert(ourDoc, callback);
        return result;
    }
}

export const Tests = new TestsCollection ('tests');

Tests.schema = new SimpleSchema({

    name: {
        type: String,
        max: 512,
        required: true,
    },
    text: {
        type: String,
        max: 512,
        required: true
    },
    testField: {
        type: Buffer,
        max: 512,
        required: true
    }

});

Tests.attachSchema(Tests.schema);

哪些通过以下方式发布:

/imports/api/server/publications.js

import { Meteor } from 'meteor/meteor';

import { Tests } from '../tests';

Meteor.publish('tests', function () {
    return Tests.find({});
});

所以现在我想将这个集合反应性地嵌入到我的模板中:

test_Tmp.html

<template name='test_Tmp'>
<h3>This is test page #1</h3>
<table>
  <tr>
     <th>name</th>
     <th>info</th>
  </tr>
  {{#unless testsReady}}
     {{ >spinner }}
  {{else}}
  {{#each getTests}}
  <tr>
      <td>{{name}}</td>
      <td>{{text}}</td>
  </tr>
  {{/each}}
  {{/unless}}
</table>
</template>

因此添加了sacha:spinner以显示等待微调框和辅助程序testsReady,这是js代码:

test_Tmp.js

import './test_Tmp.html';
import { Mongo } from "meteor/mongo";

import { ReactiveVar } from 'meteor/reactive-var';
const Tests = new Mongo.Collection('tests');

Template.test_Tmp.onCreated(function() {

    this.testsReady = new ReactiveVar(false);

    var self = this;

    var sub = this.subscribe('tests');
    Tracker.autorun(function () {
            if(sub.ready()){
                self.testsReady.set(true);
                console.log(self.testsReady.get());
            }
    })
});


Template.test_Tmp.helpers({
    testsReady: function() {
        var result = Template.instance().testsReady.get();
        console.log(result);
        return result;
    },
    getTests: function(){
        var data = Tests.find({});

        return data;

    }
});

如您所见,我使用了响应式助手testsReady,但是当我加载此页面时,我看到了Spinner,它在浏览器控制台中显示{true}消息,来自Tracker.autorun块,所以{{1} }更新为true,但是在此之后,testsReady助手没有再被调用,我的旋转器将永远旋转。有人帮忙。 我将Flow Router与BlazeLayout插件一起使用,这是我的路线:

testsReady

所有这一切都在加载路由/ tests时发生

0 个答案:

没有答案