如何检查流星模板中的空对象?

时间:2022-03-10 18:39:21

I have a template data context like this:

我有这样的模板数据上下文:

data = {
    "attribute1": {
        "attribute2": {}
    }
}

And in the meteor template I am doing something like this:

在流星模板中,我正在做这样的事情:

{{#with attribute1}}
    {{#if attribute2}}
        show some content
    {{/if}}
{{/with}}

I don't want to show anything if attribute2 is an empty object. However I tried both {{#with attribute2}}{{/with}} and {{#if attribute2}}{{/if}} And it's still rendering the content inside even if it's an empty object.

如果attribute2是一个空对象,我不想显示任何内容。但是我尝试了两个{{#with attribute2}} {{/ with}}和{{#if attribute2}} {{/ if}}并且即使它是一个空对象,它仍然会在内部呈现内容。

What is the correct way to check if the object is empty in spacebar template? Or is it even possible?

在空格键模板中检查对象是否为空的正确方法是什么?或者甚至可能吗?

3 个解决方案

#1


I just found a way to register a template helper and use jQuery.isEmpty to do the null check:

我刚刚找到了一种注册模板助手的方法,并使用jQuery.isEmpty进行空检查:

Template.registerHelper("isEmpty", function (object) {
    return jQuery.isEmpty(object);
});

And to use it in the template:

并在模板中使用它:

{{#unless isEmpty attribute2}}
    show some content
{{/unless}}

But I found a drawback from this solution that if I want to refer to attributes inside attribute2, I will need to add {{#with attribute2}}{{/with}} inside the unless block

但是我发现这个解决方案有一个缺点,如果我想引用attribute2中的属性,我需要在除非块中添加{{#with attribute2}} {{/ with}}

#2


if attribute2 is an object, you can use Object.keys to check the length

如果attribute2是一个对象,则可以使用Object.keys来检查长度

{{#with attribute1}}
    {{#if !!Object.keys(attribute2).length}}
        show some content
    {{/if}}
{{/with}}

#3


The reason why your original code does not work is because you are assuming that an empty object equates to false in Spacebars. As mentioned here in the Spacebars documentation, only falsy Javascript values (null, undefined, 0, "", and false) will be considered false by Spacebars. Therefore, an empty object needs to be checked using a Meteor helper method like your accepted answer suggests.

原始代码不起作用的原因是因为您假设空对象在空格键中等于false。正如Spacebars文档中所提到的,Spacebars只会将错误的Javascript值(null,undefined,0,“”和false)视为false。因此,需要使用Meteor辅助方法检查空对象,就像您接受的答案所示。

#1


I just found a way to register a template helper and use jQuery.isEmpty to do the null check:

我刚刚找到了一种注册模板助手的方法,并使用jQuery.isEmpty进行空检查:

Template.registerHelper("isEmpty", function (object) {
    return jQuery.isEmpty(object);
});

And to use it in the template:

并在模板中使用它:

{{#unless isEmpty attribute2}}
    show some content
{{/unless}}

But I found a drawback from this solution that if I want to refer to attributes inside attribute2, I will need to add {{#with attribute2}}{{/with}} inside the unless block

但是我发现这个解决方案有一个缺点,如果我想引用attribute2中的属性,我需要在除非块中添加{{#with attribute2}} {{/ with}}

#2


if attribute2 is an object, you can use Object.keys to check the length

如果attribute2是一个对象,则可以使用Object.keys来检查长度

{{#with attribute1}}
    {{#if !!Object.keys(attribute2).length}}
        show some content
    {{/if}}
{{/with}}

#3


The reason why your original code does not work is because you are assuming that an empty object equates to false in Spacebars. As mentioned here in the Spacebars documentation, only falsy Javascript values (null, undefined, 0, "", and false) will be considered false by Spacebars. Therefore, an empty object needs to be checked using a Meteor helper method like your accepted answer suggests.

原始代码不起作用的原因是因为您假设空对象在空格键中等于false。正如Spacebars文档中所提到的,Spacebars只会将错误的Javascript值(null,undefined,0,“”和false)视为false。因此,需要使用Meteor辅助方法检查空对象,就像您接受的答案所示。