Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:47:06+00:00 2026-06-02T15:47:06+00:00

When using backbone.js and the companion templating engine in underscore, I’ve noticed that most

  • 0

When using backbone.js and the companion templating engine in underscore, I’ve noticed that most examples call model.ToJSON() when rendering instead of just passing model. I understand that my template would have to modify how it retrieves data.

I’m wondering why & what benefit do we get from toJSON()?

Typical Example

In the typical example model.toJSON() is called when rendering. Note, for the sake of brevity, i put the template in as a string literal.

ToDoItemView = Backbone.View.extend({
   /* other viewey stuff */
   template : _.template( '<li><%=ToDoNote%></li>'),
   render   : function () {    
                   var out=  this.template(this.model.toJSON()); //<--JSON
                    $(this.el).html( out) }
                    return this;
                 }
 }); //end view

An Alternate Method

I dug through the backbone 0.9.2 & underscore 1.3.3 code. In backbone, noticed that model.toJSON() does the following: _.clone(this.attributes). Inside the template rendering engine, my compiled template names the passed-data obj.

After seeing those snippets, I realized that cloning the attributes isn’t necessary. Instead, I can directly pass in my model (albeit, with some syntax changes in the template). Something like …

ToDoItemView = Backbone.View.extend({
   /* other viewey stuff */
   template : _.template( '<li><%=obj.get('ToDoNote')%></li>'), //<--notice GET()
   render   : function () {    
                   var out=  this.template(this.model);  //<-- look ma no json
                   $(this.el).html( result ) }
                   return this;
                 }
 }); //end view

Looking at the two examples, the only reasons I can come up with to call toJSON are :

  • protect model data from a nefarious view
  • the view locally modifies data (not a good idea in my opinion),
  • view needs to access values using array/string syntax( obj[ namepart + someindex])

My question boils down to : why would I call toJSON() and take the hit for cloning the properties, rather than just use get() in my templates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:47:08+00:00Added an answer on June 2, 2026 at 3:47 pm

    Maybe the following makes sense:

    1. Interpolating instead of evaluating is a big cost to take. So your version of the template is actually a lot slower than calling toJSon() and using evalution.

    2. Logic belongs to views not templates. Introducing js code (and the need for interpolation) in templates should only be done if necessary.

    3. One could argue that you should be passing model.attributes instead of model.toJSON() avoiding the clone. I guess the reason for not doing so is to avoid allowing the template to change the model’s attributes. In addition, you might typically want to augment the result of model.toJSON() with other things, which obviously you don’t want to do on the model.attributes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using backbone's underscore templating engine with the mustache formatting patterns. I have already
I'm using backbone.js and rails. When backbone.js saves a model for the first time
I'm using Backbone and therefore Underscore to render my templates. My templates get rendered
I'm developing application using backbone.js & jquery. I have following code in model: runReport:
I'm using backbone with the backbone-rails gem which does its own templating and project
We are using Backbone.js to build client-side UI that manipulates models shared by multiple
I'm building an app using Brunch and Backbone.js that is to include nested menus.
Using the following as an example: var Case = Backbone.Model.extend({ initialize: function(){ this.bind(error, function(model,
I am using backbone.js and underscore.js to build an javascript application. Since hours of
Using backbone, I'd like to fetch data from a couchdb server that is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.