正确处理古腾堡WordPress核心数据检索

时间:2021-11-24 作者:Matt Woodward

我正在尝试通过Gutenberg边栏插件中的slug名称获取标签/类别的ID。我创建了下面的函数,但第一次返回null的结果仍然很困难(由于GetEntityRecords后面的API调用的延迟)。第二次调用数据时显示正确。

有什么想法吗?

PS:执行此功能;onClick“;我的组件中的按钮。代码当前位于组件外部。

function GetTaxonomyId(taxonomy_slug, type){

    //Setup Locals
    let taxonomy_object = null;

    //Setup Queries
    const query = { slug: taxonomy_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)) {
            
            console.log(\'resolved\');

            //Get values from the cache
            taxonomy_object = select(\'core\').getEntityRecords(\'taxonomy\', type, query);

            // We\'re done, so let\'s unsubscribe from the isResolving() check above.
            unsubscribe();

            //console.log(JSON.stringify(taxonomy_object)); //<-- This Shows as an empty object
        
            //Verify we have a valid object
            if(taxonomy_object){
                console.log(\'Object ID is \' + taxonomy_object[0].id);
                //return taxonomy_object[0].id;
            }
                      
        }else{
            console.log(\'still resolving\');
        }
    })
}
Ref:

  1. How to use “getEntityRecords” for user data?

1 个回复
最合适的回答,由SO网友:Matt Woodward 整理而成

所以通过一些尝试和错误,我发现为了检测并确保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 有关自定义函数的详细信息。

相关推荐

迁移到新域名后出现JavaScript/AJAX错误

我已经将一个客户的站点从旧的垃圾域名迁移到新的域名,以更好地代表他们的业务。然而,自从移动以来,似乎出现了某种Javascript/Ajax问题。例如,我似乎无法切换选项卡(在ajax页面上),而;“快速编辑”;工具根本不起作用。当单击任何页面上的“快速编辑”链接时,我在F12控制台中发现此错误。Uncaught TypeError: Cannot read property \'hasClass\' of undefined at HTMLDocument.<anonymous&g