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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:04:19+00:00 2026-06-06T13:04:19+00:00

Let me show what I need first so you can understand my problem. I

  • 0

Let me show what I need first so you can understand my problem.
I have a contact view like:

ContactView = Backbone.View.extend({
    template: _.template("Name: <%= name %>
                          E-mail: <%= email %>
                          Phones
                          <%= label1 %> - <%= number1 %>
                          <%= label2 %> - <%= number2 %>"),
    render: function() {
       var contact = this.template(this.model.toJSON());
       this.$el.html(contact);
    }
});

So far so good, the problem is that the phones part is a list, my model holds an array of phones, and I need to put it dynamically in that template.

What I thought was to create another model and view Phone and PhoneView just for this.
And then in the ContactView I would create a method render_phones, like:

render_phones: function() {
    var phones = this.model.get('phones');
    var phones_str = "";
    for (var i in phones) {
        var phone = new Phone(phones[i]);
        var phoneView = new PhoneView({model: phone});
        phones_str += phoneView.render();
    }
    return phones_str;
}

I changed the ContactView template to this:

template: _.template("Name: <%= name %>
                      E-mail: <%= email %>
                      Phones
                      <%= phones %>"),

But how to make my render method get the phone list rendered and put inside the template?

I couldn’t find a pattern to deal with situations like this. And the code is starting to look not so good.

ps: This is just an example, there are several other parts of my application that I need this, a view that need to generate subviews with content in a list.

  • 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-06T13:04:20+00:00Added an answer on June 6, 2026 at 1:04 pm

    It looks like you are using underscore.js to render your templates, underscore.js provides a way for rendering a collection within a template (look at the second example). for example

    template: _.template("Name: <%= name %>
                              E-mail: <%= email %>
                              Phones
                    <% _.each(phones, function (phone) { %>
                     <%= label %> - <%= number %>
                    <% }); %>"),
    

    You can also render an individual view for each model and in the same render model modify the el to render it there, but unless you need the view (for example the content will change or you’re listening to events) it probably doesn’t pay to do it that way. Here’s an example of how you could accomplish that

    render: function () {
      this.$el.html(this.model.toJSON());
      _.each(this.model.get('phones'), function(phone) {
        this.$el.find('.phoneArea').append(new phoneView({model: phone}).render().el));
      },this);
      return this;
    }
    

    Also if you were to go this route then note that it would probably pay to cache the views, but again if all you need is to render the content then it would probably pay to just keep the phone numbers as an array and render it within the template.

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

Sidebar

Related Questions

a funny problem ... i can't understand it.. let me show you what i
Specifically how can I: Show a button which will let the user browse through
Let's say that I have an application frame, and I want to show a
I can't seem to find anything that would let me to easily show a
Is there any way to have a default MessageBox.Show() caption? Let says I would
Hey all, let's jump straight to a code sample to show how ECMAScript/JavaScript/AS3 can't
I have a program to process very large files. Now I need to show
I have one problem with semaphores, I though you could help me.. Okay first
Let's say I have numbers like 123456000000 12345000000 123456000 123456 and I want them
Let me first say that I have quite a lot of Java experience, but

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.