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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:50:20+00:00 2026-06-06T20:50:20+00:00

I have a few different problems going on, I hope though this example is

  • 0

I have a few different problems going on, I hope though this example is easy to follow. The code uses an HTML template with elements hidden by default (using CSS). The Backbone View uses data in a Model to display appropriate values OR hide the UI element if no value is present in the Mode.

Given a template where everything is hidden by default (using CSS), for example:

<script type="text/template" id="Person-Template">
    <span class="fname" title="FirstName"></span>
    <span class="lname" title="LastName"></span>
    <span class="age" title="Age"></span>
</script>

To hide each UI element the CSS is:

span.fname,
span.lname,
span.age {
    display:none; 
}

My Backbone.js Model would therefore be:

PersonModel = Backbone.Model.extend({
        defaults: {
            fname: undefined,
            lname: undefined,
            age: undefined
        }
    });

The View (simplified) would be:

PersonView = Backbone.View.extend({
    tagName: 'div',

    initialize: function() {
        this.model.on("fname", this.updateFName, this);
        this.model.on("lname", this.updateLName, this);
        this.model.on("age", this.updateAge, this);
    },

    updateFName: function() {
        // Pseudo code 
        Get 'new' value from Model
        Obtain reference to DOM element span.fname
        Update span.fname

        if (model value is empty) {
           Hide UI element.
        }
    },

    updateLName: function() {
        // Same as above
    },

    updateAge: function() {
        // Same as above
    },

    render: function() {
        // Get model values to display
        var values = {
            FirstName : this.model.get('fname'),
            LastName : this.model.get('lname'),
            Age: this.model.get('age'),
        };

        // Load HTML template
        var template = $('#Person-Template').html();

        // Populate template with values
        var t = _.template(template, values);

       // Show / hide UI elements
       this.updateFname();
       this.updateLName();
       this.updateAge();
    }

}

Finally, the question: It seems hacky calling each updateXYZ() method from render() just to determine whether the UI element should be set to hidden or visible. I have a lot of attributes in my model and the code just seems a little absurd really.

I have been told on SO that the View should not be responsible for determining what should or should be displays. My questions is, well then what is responsible? The user may perform some (valid) aciton which clears the First Name, in which case I don’t want my View displaying ‘First name:’ followed by no value.

  • 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-06T20:50:21+00:00Added an answer on June 6, 2026 at 8:50 pm

    First of all, you don’t need to build your values by hand, just use toJSON:

    var values = this.model.toJSON();
    

    Then, you have to add your filled in template to your view’s el:

    this.$el.html(_.template(template, values));
    

    and your template should probably include something to display in your template:

    <script type="text/template" id="Person-Template">
        <span class="fname" title="FirstName">F: <%= fname %></span>
        <span class="lname" title="LastName">L: <%= lname %></span>
        <span class="age" title="Age">A: <%= age %></span>
    </script>
    

    You don’t separate functions for each of the three parts, you could just loop through them in your render:

    _(values).each(function(v, k) {
        var m = _(v).isUndefined() ? 'hide' : 'show';
        this.$('.' + k)[m]();
    }, this);
    

    Now back to your events. There is no such thing as an "fname" event unless you’ve added a custom one. But there’s no need for that, the model will trigger "change" and "change:fname" events when the fname is changed; you only need to care about "change" though:

    initialize: function() {
        _.bindAll(this, 'render');
        this.model.on("change", this.render);
    },
    

    I’ve also bound render to your view instance using _.bindAll so that you don’t have to worry about the third argument to this.model.on.

    Now you have something that works: http://jsfiddle.net/ambiguous/46puP/


    You can also push the “should this be displayed” logic into the template:

    <script type="text/template" id="Person-Template">
        <% if(fname) { %><span class="fname" title="FirstName">F: <%= fname %></span><% } %>
        <% if(lname) { %><span class="lname" title="LastName">L: <%= lname %></span><%  } %>
        <% if(age)   { %><span class="age" title="Age">A: <%= age %></span><%           } %>
    </script>
    

    and simplify your render:

    render: function() {
        var values   = this.model.toJSON();
        var template = $('#Person-Template').html();
        this.$el.html(_.template(template, values));
        return this;
    }
    

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

    This approach would probably be the most common and there’s nothing wrong with it. I think you’re misunderstanding what the previous answer was trying to tell you. The template chooses what pieces of information to display through <%= ... %> already so there’s no good reason that it shouldn’t see if fname, for example, is set before trying to display it. Depending on the nature of your data, you might want to use if(!_(fname).isUndefined()) and such in your template but a simple truthiness check is probably fine; the age might be an issue in some cases though so you might want to be a bit stricter with that.

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

Sidebar

Related Questions

I have a few different applications that i need to share code between to
I have tried this a few different ways and it always seems to fail.
I am writing an application, where I do have few different windows implemented, where
I have a few different things open in the terminal whenever I'm developing --
We have a few different programs all compiled together in the same suite, recently
I have a few different classes which origin is a another class. I have
Background : I have a few different threads which each need to write to
I have defined a few different functions in my .bash_profile . I usually remember
I have been researching a few different apps lately for my company which are
I have made up a few different custom cells in my UITableView in Interface

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.