检查数组中对象的属性

时间:2016-01-25 19:15:13

标签: backbone.js underscore.js backbone-views

如果办公室有租用空间,我需要在模板中写出一个标题。基本上我只是想把它拿出来:

<div>Office Unit
    {% if (m.hasRentalSpace) { %}
        <h2>Office space available!</h2>
    {% } else { %}
        <h3>There are currently no office units for rent in this building.</h3>
    {% } %}
</div>

在模型中,租赁单元是一个数组,我需要遍历数组,并检查它们的状态是否为4(这意味着可用空间)。

所以我想在我的骨干模型中做这样的事情:

   hasRentalSpace: function() {
        this.Office.each( function(o) {
            console.log('o is: ', o.status);
        });
    },
问题是,对于每个&#39;,我需要访问一个名为“状态”的属性。但如果我通过上面的测试检查它, 我每次都得到不确定。但是,如果我只写出&#39; o,我可以在conole中看到状态属性,如下所示:

enter image description here

所以,问题是,如何在迭代数组时获取对象的状态?

谢谢!

一旦我解决了逻辑,我就可以在我的视图中使用hasRentalSpace函数,如下所示:

在我看来:

render: function () {
this.$el.html(this.template(hasRentalSpace: this.model.hasRentalSpace()));
    return this;
},

1 个答案:

答案 0 :(得分:2)

假设办公室是骨干模型(屏幕截图似乎就是这种情况),而不是

console.log('o is: ', o.status);

你应该使用

console.log('o is: ', o.get("status"));

相关问题