json - Backbone toJSON not rendering -


when use backbone tojson method of model this:

this.$el.html(this.model.tojson()); 

it doesn't render model view root element ( more 1 attribute ).

but when 1 property model, this;

 this.$el.html(this.model.get("city")); 

it rendered properly.

also, when use template in first case (tojson) - rendered fine.

this.$el.html(this.template(this.model.tojson()); 

why ?

thanks

so, this.template (in of cases) compiled version of html template have view.

it have placeholders in it, , take parameters same key placeholders in template. example (handlebars templates),

<section id="{{id}}">   <header>{{header_text}}</header> </section> 

considering above code template, when compile , store in this.template, returns function, takes json object parameter, this.template function.

you can call below,

var html_text = this.template({   id : "main_content",   header_text : "hi welcome !!" });  this.$el.html(html_text); 

after execution, el's contents be

<section id="main_content">   <header>hi welcome !!</header> </section> 

so when this.$el.html(this.template(this.model.tojson());, generates required json parameter this.template method you, hence works fine.

and loamhoof said, in this.$el.html(this.model.get("city")); use html method set html content of el based on property value of model.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -