Search code examples
javascriptjsonbackbone.jsunderscore.jsunderscore.js-templating

How to render is TREE JSON using underscore.js each loop?


I created collection inserting type of JSON TREE.

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;
      }

My collections are set they are the picture below. enter image description here

I want to know how access the model.attributes (that is mean how to access each object?)

I entered console.log statement that's likeconsole.log(model.attributes.bathroom);

The results are shown below. enter image description here

How to access this attribute using underscore.js each in html??
I really want that its solution.



Solution

  • try this, maybe it is what you want

    _.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]);
           }
          }
    
        });