所以通过一些尝试和错误,我发现为了检测并确保API调用的结果(以及随后的状态更改)得到正确处理,我的一些逻辑出现了错误。
我怀疑;“其他”;状态发生了变化,我在需要之前就取消了订阅-因此取消订阅已移动到检查我所需数据的例程中。
我已经为其他遇到此问题的人发布了以下解决方案:
function ProcessTaxonomy(taxonomy_slug, type){
//Setup Locals
let taxonomy_object = null;
//Setup Queries
const query = { slug: taxonomy_slug, _fields: \'id,slug\' };
//Get terms
select(\'core\').getEntityRecords(\'taxonomy\', type, query);
//Subscribe to state changes for isResolving
const unsubscribe = subscribe(() => {
const { isResolving } = select( \'core/data\' );
//Build Args object
const args = [\'taxonomy\', type, query];
//Verify the queries have resolved
if ( !isResolving(\'core\', \'getEntityRecords\', args)) {
//Get values from the cache
taxonomy_object = select(\'core\').getEntityRecords(\'taxonomy\', type, query);
//Check length of array (do we have data to work with?)
let length = (taxonomy_object ? taxonomy_object.length : 0);
//Verify we have a valid object
if(length > 0){
// We\'re done, so let\'s unsubscribe from the isResolving() check above.
unsubscribe();
//Extract Taxonomy Item ID
let taxonomy_item_id = taxonomy_object[0].id;
// Add Taxonomies to UI
switch (type) {
case \'post_tag\': //Tags
AddTag(taxonomy_item_id); // Custom function to add tag to post and refresh the tag panel
break;
case \'category\': //Categories
AddCategory(taxonomy_item_id); // Custom function to add category to post and refresh the category panel
break;
default:
break;
}
}
}else{
// Still resolving
}
})
}
这可能会触发,需要花费一定的时间来解析,然后根据需要更新帖子/用户界面(使用自定义的AddTag/AddCategory函数)。
看见this question on SO 有关自定义函数的详细信息。