【发布时间】:2015-11-08 01:25:00
【问题描述】:
在 Meteor 应用程序中,通过单击运行的一项功能需要一段时间才能在某些速度较慢的设备上运行。因此,在这些速度较慢的设备上,应用程序似乎直到功能完成后才执行任何操作。
有问题的函数循环通过一个较大的数组。它不做任何外部事情或方法调用。
为了直观地了解用户,我想向用户显示一个微调器(在慢速设备上)。理想情况下,我会在活动开始时显示微调器,然后在活动结束时移除微调器。但是,如果我对 Meteor 的理解是正确的(并且基于我尝试它时似乎发生的情况),那么在事件期间指定的所有模板更新都只会在事件结束时传播。因此,微调器永远不会显示。
我怎样才能使这项工作按预期进行?
当前设置(使用实际代码编辑):
在 category.html 中:
{{#if hasSubCategories}}
<a href='#_' id='categorySelect-{{id}}' class='btn categorySelect pull-right
{{#if allSubsSelected}}
btn-danger
{{else}}
btn-success
{{/if}}
'>{{#if isFlipping}}<span id='spinner-{{id}}'>flip</span>{{/if}}<span class="glyphicon
{{#if allSubsSelected}}
glyphicon-remove
{{else}}
glyphicon-ok
{{/if}}
" aria-hidden="true"></span></a>
{{/if}}
在 category.js 中:
Template.categories.events({
"click .categorySelect": function (event) {
Session.set('categorySpinner', this.id);
categoryFlipper(this.id, function() {
Session.set('categorySpinner', "");
});
return false;
},
});
Template.categories.helpers({
allSubsSelected: function() {
var finder = Categories.find({parentId: this.id});
var allSelected = true;
finder.forEach(function(item) {
if (!($.inArray(item.id, Session.get("categoriesSelected")) !== -1)) {
allSelected = false;
}
});
return allSelected;
},
isFlipping: function() {
if (Session.get("categorySpinner") == this.id)
return true;
else
return false;
}
});
在 main.js 中:
categoryFlipper = function (id, callback) {
var finder = Categories.find({parentId: id});
var allSelected = true;
finder.forEach(function(item) {
if (!($.inArray(item.id, Session.get("categoriesSelected")) !== -1)) {
allSelected = false;
}
});
var t = Session.get("categoriesSelected");
if (allSelected) {
finder.forEach(function(item) {
t.splice($.inArray(item.id, t), 1);
});
}
else {
finder.forEach(function(item) {
if (!($.inArray(item.id, t) !== -1)) {
t.push(item.id);
}
});
}
Session.set("categoriesSelected", t);
callback();
}
【问题讨论】: