0

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.


4
  • I'm not sure that I fully understand your question but, it seems like you don't know how to browse the inner object of your model attributes? Commented Mar 7, 2017 at 13:30
  • Yes, it's correct what you said. Commented Mar 7, 2017 at 13:43
  • ok is model.attributes an array or an object? Commented Mar 7, 2017 at 13:45
  • My model was consist of object! Commented Mar 7, 2017 at 13:47

2 Answers 2

1

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

    });
Sign up to request clarification or add additional context in comments.

2 Comments

yes! that's right way but, can I ask you one more thing? how to use model.attributes.hasOwnProperty in html? for example, <% _.each(result, function(model){ %> ?
yes something like that, read this article bennadel.com/blog/… I think that's what you are looking for
0

simply :

model.get('bathroom');

1 Comment

Hmm.. I didnt try that lol i will go home and try your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.