Pybossa JS - taskLoaded() 函数在获取 1 个任务时执行两次
Pybossa JS - taskLoaded() function executes twice when fetching 1 task
我目前正在为我的 PYBOSSA 项目构建一个自定义任务展示器。我几乎已经实现了它,但仍停留在以下 javascript 函数 -
pybossa.taskLoaded(function(task, deferred) {
if ( !$.isEmptyObject(task) ) {
console.log("Hello from taskLoaded");
// load image from flickr
var img = $('<img />');
img.load(function() {
// continue as soon as the image is loaded
deferred.resolve(task);
pybossaNotify("", false, "loading");
});
img.attr('src', task.info.url).css('height', 460);
img.addClass('img-thumbnail');
task.info.image = img;
console.log("Task ##"+task.id);
}
else {
deferred.resolve(task);
}
});
根据文档 -
The pybossa.taskLoaded method will be in charge of adding new items to the JSON task object and resolve the deferred object once the data has been loaded (i.e. when an image has been downloaded), so another task for the current user can be pre-loaded.
但是请注意我的功能。我已经记录了任务 ID,函数加载。它加载 2 个任务。登录后,控制台显示 -
Task ##256
Task ##257
我也尝试过其他各种说法。他们也执行两次。我的想法是,如果现在我尝试插入当前任务的问题,那么下一个任务的功能也将与其各自的图像一起放置。我该如何解决?
你看到双倍有一个很好的理由:-) PYBOSSA 预加载下一个任务,所以最终用户确实认为下一个任务加载得非常快(实际上是即时的)。
虽然对于某些项目这可能不是问题,但在某些情况下用户需要下载大图像、检查其他 API 等,因此在呈现之前需要 2 或 3 秒(甚至更多)来获取所有内容给用户的任务。
PYBOSSA.JS 处理这种情况,一旦数据被下载,它就会请求一个新任务,但不是呈现它,而是在你的 window 中。当您构建自己的模板时,您必须将该数据添加到 dom(通过隐藏元素),然后在 pybossa.presentTask 方法中,您将检查正在加载哪个任务,并且 show/hide 上一个.
在pybossa.saveTask中,可以删除前面的DOM个元素。
我希望现在更清楚了。如果你不想这样,你可以使用 jQuery 或 Axios 来请求一个任务,保存它并在你需要时加载下一个 ;-)
我目前正在为我的 PYBOSSA 项目构建一个自定义任务展示器。我几乎已经实现了它,但仍停留在以下 javascript 函数 -
pybossa.taskLoaded(function(task, deferred) {
if ( !$.isEmptyObject(task) ) {
console.log("Hello from taskLoaded");
// load image from flickr
var img = $('<img />');
img.load(function() {
// continue as soon as the image is loaded
deferred.resolve(task);
pybossaNotify("", false, "loading");
});
img.attr('src', task.info.url).css('height', 460);
img.addClass('img-thumbnail');
task.info.image = img;
console.log("Task ##"+task.id);
}
else {
deferred.resolve(task);
}
});
根据文档 -
The pybossa.taskLoaded method will be in charge of adding new items to the JSON task object and resolve the deferred object once the data has been loaded (i.e. when an image has been downloaded), so another task for the current user can be pre-loaded.
但是请注意我的功能。我已经记录了任务 ID,函数加载。它加载 2 个任务。登录后,控制台显示 -
Task ##256
Task ##257
我也尝试过其他各种说法。他们也执行两次。我的想法是,如果现在我尝试插入当前任务的问题,那么下一个任务的功能也将与其各自的图像一起放置。我该如何解决?
你看到双倍有一个很好的理由:-) PYBOSSA 预加载下一个任务,所以最终用户确实认为下一个任务加载得非常快(实际上是即时的)。
虽然对于某些项目这可能不是问题,但在某些情况下用户需要下载大图像、检查其他 API 等,因此在呈现之前需要 2 或 3 秒(甚至更多)来获取所有内容给用户的任务。
PYBOSSA.JS 处理这种情况,一旦数据被下载,它就会请求一个新任务,但不是呈现它,而是在你的 window 中。当您构建自己的模板时,您必须将该数据添加到 dom(通过隐藏元素),然后在 pybossa.presentTask 方法中,您将检查正在加载哪个任务,并且 show/hide 上一个.
在pybossa.saveTask中,可以删除前面的DOM个元素。
我希望现在更清楚了。如果你不想这样,你可以使用 jQuery 或 Axios 来请求一个任务,保存它并在你需要时加载下一个 ;-)