我希望你帮助我。当我改变订阅时会发生一些奇怪的事情。因此,订阅中未显示的数据会显示一会儿。在这里我的消息来源:
1.自动运行等待分页:
template.autorun(function (computation) {
if (Session.get('factors.page.ready') || Session.get('rules.page.ready')) {
var factors;
var userIds = Factors.find({},{sort :Session.get('factors.sort')}).map(function (factor) {
return factor.creator;
});
Meteor.setTimeout(function () {
factors = Factors.find({},{sort :Session.get('factors.sort')}).fetch();
Meteor.call('factors.getUserCount', _.pluck(factors, '_id'), function (err, counts) {
if (err) return;
_.forEach(counts, function (obj) {
var factor = _.find(factors, function (factor) {
return factor._id === obj._id;
});
if (factor) factor.userCount = obj.count;
});
if(!computation.firstRun) {
template.factors.set(factors);
}
});
}, 15);
template.subscribe('users.names', { _id: { $in: userIds } });
}
});
也是分页autoruns:
template.autorun(function () {
var options = Session.get(collection + '.options') || {};
var sort = Session.get(collection + '.sort');
var page = Session.get(collection + '.page') || template.page.get() || 1;
if (!options.limit || typeof options.limit !== 'number') {
options.limit = 10;
}
options.skip = (page - 1) * options.limit;
if (sort) options.sort = sort;
// create a subscription with parameters
sub = Meteor.subscribe(collection, {
filters: Session.get(collection + '.filters'),
options: options,
assignFirst: Session.get(collection + '.assignFirst')
});
});
template.autorun(function () {
var page = template.page.get() || 1;
var options = Session.get(collection + '.options') || {};
var limit = options.limit || 10;
var visiblePagesNum = options.visiblePagesNum || 7;
var visiblePagesHalf = (0 | (visiblePagesNum - 1) / 2);
var visiblePages = [];
// wait for ready
if (sub.ready()) {
count = Counts.get(collection) || limit;
// get array of pages, i.e. [1, 2, 3...]
pages = _.range(1, Math.ceil(count / limit) + 1);
if (page - visiblePagesHalf < 1){
visiblePages = _.first(pages, visiblePagesNum);
}else{
if(page + visiblePagesHalf > pages.length){
visiblePages = _.last(pages, visiblePagesNum);
}else{
visiblePages = _.range(page - visiblePagesHalf, page + visiblePagesHalf + 1);
}
}
// set first page if there is only one
if (page !== 1 && (pages.length === 1)){
template.page.set(1);
}
Session.set(collection + '.page', page);
// set amount of pages for pagination
template.pages.set(pages);
// set visible pages
template.visiblePages.set(visiblePages);
if (collection === 'logs') {
Meteor.setTimeout(function() {
Session.set(collection + '.page.ready', true);
}, 200);
} else {
Session.set(collection + '.page.ready', true);
}
} else {
Session.set(collection + '.page.ready', false);
}
});
在页面上传输数据的助手:
factors: function () {
return Template.instance().factors.get();
},
抱歉我的英文