流星 collections 刷新时不加载
meteor collections don't load on refresh
出于某种奇怪的原因,我根本无法在本地重现,meteor 在刷新页面时不会加载任何 collections,但它们在状态与状态之间的转换中工作得很好。我已经向 collection 添加了一个 onready 和 onerror 函数,但它们实际上都没有 运行 刷新。
this previous so question seems to solve the problem. 我无法像他那样解决问题,因为显然 angular 的 ui 路由器不尊重等待和数据。因此,link 对我没有多大用处。几天来我一直在努力解决这个问题,但我一无所获。在此先感谢您的帮助。
我确定我在这里发布的代码太多了,但我宁愿是全面的。
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();
}
}
})
原来你绝对可以通过路由器解决这个问题,使用以下方法:
user: ($auth) => {
...
},
meetings: ['$q', ($q) => {
var deferred = $q.defer();
Meteor.subscribe('meetings', {
onReady: deferred.resolve,
onStop: deferred.reject
});
return deferred.promise;
}]
出于某种奇怪的原因,我根本无法在本地重现,meteor 在刷新页面时不会加载任何 collections,但它们在状态与状态之间的转换中工作得很好。我已经向 collection 添加了一个 onready 和 onerror 函数,但它们实际上都没有 运行 刷新。
this previous so question seems to solve the problem. 我无法像他那样解决问题,因为显然 angular 的 ui 路由器不尊重等待和数据。因此,link 对我没有多大用处。几天来我一直在努力解决这个问题,但我一无所获。在此先感谢您的帮助。
我确定我在这里发布的代码太多了,但我宁愿是全面的。
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();
}
}
})
原来你绝对可以通过路由器解决这个问题,使用以下方法:
user: ($auth) => {
...
},
meetings: ['$q', ($q) => {
var deferred = $q.defer();
Meteor.subscribe('meetings', {
onReady: deferred.resolve,
onStop: deferred.reject
});
return deferred.promise;
}]