【问题标题】:How to render is TREE JSON using underscore.js each loop?如何在每个循环中使用 underscore.js 渲染 TREE JSON?
【发布时间】:2017-03-07 13:24:11
【问题描述】:

我创建了 JSON 树的集合插入类型。

view.js

views.Livingword = Backbone.View.extend({
      render: function(templateName) {
        var template = _.template(templateName);
        this.$el.html(template({result : this.collection.models}));
        _.each(this.collection.models, function(model){
          console.log(model.attributes.bathroom);
        });
        return this;
      }

我的收藏集如下图所示。

我想知道如何访问model.attributes(也就是说如何访问每个对象?)

我输入了类似于console.log(model.attributes.bathroom);console.log 语句

结果如下所示。

如何在 html 中使用 underscore.js each 访问该属性??
我真的很想要它的解决方案。


【问题讨论】:

  • 我不确定我是否完全理解您的问题,但是您似乎不知道如何浏览模型属性的内部对象?
  • 是的,你说的没错。
  • ok 是model.attributes一个数组还是一个对象?
  • 我的模型是由对象组成的!

标签: javascript json backbone.js underscore.js underscore.js-templating


【解决方案1】:

试试这个,也许这是你想要的

_.each(this.collection.models, function(model){
      console.log(model.attributes.bathroom); 
      for(var i in model.attributes){
       if (model.attributes.hasOwnProperty(i)){
        console.log(model.attributes[i]);
       }
      }

    });

【讨论】:

  • 是的!这是正确的方式,但是,我可以再问你一件事吗?如何在 html 中使用 model.attributes.hasOwnProperty?例如, ?
  • 是的,类似的,阅读这篇文章bennadel.com/blog/…我想这就是你要找的东西
【解决方案2】:

简单地说:

model.get('bathroom');

【讨论】:

  • 嗯..我没试过,哈哈,我会回家试试你的答案。
猜你喜欢
  • 1970-01-01
  • 2011-11-13
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2020-11-14
  • 2013-06-04
相关资源
最近更新 更多