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 8127613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:35:33+00:00 2026-06-06T07:35:33+00:00

Assume that I have a backbone model that has a bunch of boolean attributes:

  • 0

Assume that I have a backbone model that has a bunch of boolean attributes:

Car = Backbone.Model.extend({});

car_one = new Car({
    a_c: true,
    mp3: true,
    gps: false,
    touchscreen: false,
    // etc...
})

I want to be able to render a list of these boolean attributes, and have an icon beside them, depending on true or false. If true, the icon will be a green tick, otherwise, display a red X icon.

Something in the lines of:

<ul>
<li><img src="tick.png">AC</li>
<li><img src="tick.png">MP3</li>
<li><img src="cross.png">Gps</li>
<li><img src="cross.png">Touch screen</li>
</ul>

Is there a better way to do this, instead of wrapping each li in an if statement in the template:

<% if (model.a_c === true) { %>
    // show tick...
<% } else { %>
   // show red cross..
<% } %>

I have approximately 12 boolean attributes that need to be rendered like this…

  • 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-06T07:35:35+00:00Added an answer on June 6, 2026 at 7:35 am

    You can access JavaScript functions from within your templates. So you could put something in window (i.e. the global scope):

    window.underscore_helpers = {
        list_of_booleans: function(obj, bools) {
            // 'obj' is the object for the template, 'bools'
            // is an array of field names. Loop through 'bools'
            // and build your HTML...
            return the_html_li_elements;
        }
    };
    

    Then you’ll want to take advantage of the variable option to _.template:

    By default, template places the values from your data in the local scope via the with statement. However, you can specify a single variable name with the variable setting. This can significantly improve the speed at which a template is able to render.

       _.template("<%= data.hasWith %>", {hasWith: 'no'}, {variable: 'data'});
       => "no"
    

    Then you can have something like this in your template:

    <%= underscore_helpers.list_of_booleans(
        json,
        ['a_c', 'mp3', 'gps', 'touchscreen']
    ) %>
    

    and use your template like this:

    var html = _.template($('#t').html(), model.toJSON(), { variable: 'json' });
    // or
    var tmpl = _.template($('#t').html(), null, { variable: 'json' });
    var html = tmpl(model.toJSON());
    

    Demo: http://jsfiddle.net/ambiguous/Yr4m5/

    By using the variable option you’ll have to say <%= json.attribute %> instead of <%= attribute %> but that’s pretty minor.

    You could use a similar approach to format the <li>s one by one and keep more of the HTML in the template.

    Another option is to throw a for loop into your template, something like this:

    <script id="t" type="text/x-underscore-template">
        <ul>
            <% var fields = ['a_c', 'mp3', 'gps', 'touchscreen' ] %>
            <% for(var i = 0; i < fields.length; ++i) { %>
                <li class="<%= json[fields[i]] ? 'true' : 'false' %>"><%= fields[i] %></li>
            <% } %>
        </ul>
    </script>​​​​​​​​​​​​​​​​​​​​
    

    Demo: http://jsfiddle.net/ambiguous/983ks/

    You’ll notice that this uses the variable: 'json' option as well, you need that so that you’ll have something to use the [] on to grab a field by name when the name is in a variable.
    This al

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

Sidebar

Related Questions

I have noticed that when multiple attributes of a Backbone model are set like
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have an invoice model, that has many invoice items. I have a form
Frustratingly, most ‘tutorial’ examples of backbone.js apps assume a clean model slate. I-.e. that
Assume a Backbone model with the following attributes: - subtotal - discount - total
Assume that we have two sets: A=(a_1,a_2,...,a_m) and B=(b_1,b_2,...,a_n) (Not necessarily of same size).
Assume that i have created a client TCP Socket in Activity A. I want
Assume that I have 2 .php files : index.php and ajax.php This is index.php:
Assume that we have a large file which can be read in chunks of
Assume that I have login to system using PHP + MySQL, then, after I

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.