简体   繁体   中英

meteor collections don't load on refresh

for some strange reason that i just can't reproduce locally at ALL, meteor doesn't load any collections when refreshing the page, but they work just fine transitioning from state to state. i've added an onready and onerror function to the collection, but neither of them actually run on refresh.

this previous so question seems to solve the problem. i'm unable to resolve the issue the way he did since apparently angular's ui router doesn't respect waiton and data. as such, that link hasn't been much use to me. i've been trying to solve this for a handful of days and i've gotten nowhere. thanks in advance for the help.

i'm sure i'm posting way too much code here, but i'd rather be comprehensive.

collections/meetings.js

Meetings = new Mongo.Collection('meetings');

Meetings.allow({
  insert: function (userId, doc) {
    return userId && doc.owner === userId;
  },
  update: function (userId, doc, fields, modifier) {
    return userId && doc.owner === userId;
  },
  remove: function (userId, doc) {
    return userId && doc.owner === userId;
  }
});

Meetings.before.insert(function(userId, doc) {
  doc.created_at = new Date().getTime();
  doc.owner = userId;
  doc.private = true;
});

Meetings.before.update(function(userId, doc, fieldNames, modifier, options) {
  modifier.$set = modifier.$set || {};
  modifier.$set.updatedAt = new Date().getTime();
});

publishes/meetings.js

Meteor.publish("meetings", function () {
  return Meetings.find({});
});

meetingController.js

  function meetingCtrl($scope, $location, $reactive, $http) {
    const vm = this;
    vm.me = Meteor.userId();
    vm.lastWeek = moment().add(-1, 'weeks').unix();

    $reactive(vm).attach($scope);

    vm.subscribe('meetings', () => [], {
      onReady: function () { console.log("onReady", arguments); },
      onError: function () { console.log("onError", arguments); }
    });

    vm.helpers({
      meetings: () => Meetings.find(
        {$and: [
          {created_at: {$gte: this.getReactively('lastWeek')}},
          {owner: this.getReactively('me')}
        ]})
    });

index.ng.html

  <div ng-repeat="meeting in vm.meetings" class="meeting">
    {{meeting.name}}
  </div>

router.js

.state('meetings', {
  url: '/meetings',
  controller: 'meetingCtrl',
  controllerAs: 'vm',
  templateUrl: baseUrl + 'meetings/' + template,
  resolve: {
    user: ($auth) => {
      return $auth.requireUser();
    }
  }
})

turns out you absolutely can resolve this issue via the router, using the following:

    user: ($auth) => {
        ...
    },
    meetings: ['$q', ($q) => {
      var deferred = $q.defer();

      Meteor.subscribe('meetings', {
        onReady: deferred.resolve,
        onStop: deferred.reject
      });

      return deferred.promise;
    }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM