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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:38:44+00:00 2026-06-06T18:38:44+00:00

I’m rather new to Backbone.js development, and have run into a bit of a

  • 0

I’m rather new to Backbone.js development, and have run into a bit of a roadblock while attempting to render a subview.

Currently, I have in place several views to render a custom dropdown-button, as well as other elements. I’ve taken this approach based on DocumentCloud’s code

Here’s what I have so far:

app.ui.SelectMenu = Backbone.View.extend({

    className: 'btn-group group-item',

    options: {
        id: null,
        standalone: false
    },

    events: {
        "click .dropdown-menu a": "setLabel"
    },

    constructor: function (options) {
        Backbone.View.call(this, options);

        this.items = [];
        this.content = JST['common-select_button'];
        this.itemsContainer = $('.dropdown-menu', $(this.content.render()));

        // Add any items that we may have added to the object params
        if (options.items) {
            this.addItems(options.items);
        }
    },

    render: function () {
        this.$el.html(this.content.render({
            label: this.options.label,
            items: this.itemsContainer
        }));
        this._label = this.$('.menu-label');
        return this;
    },

    setLabel: function (label) {
        $(this._label).text(label || this.options.label);
    },

    addItems: function (items) {
        this.items = this.items.concat(items);
        var elements = _(items).map(_.bind(function (item) {
            var attrs = item.attrs || {};
            _.extend(attrs, { 'class': 'menu_item' + (attrs['class'] || '') });

            var el = this.make('li', attrs, item.title);
            return el;
        }, this));

        $(this.itemsContainer).append(elements);
    }
});

So far I have successfully rendered my button, as well as the appropriate label, but I cannot seem to populate the .dropdown-menu when calling the addItems function.

I’m assuming that when render hits, the items variable cannot be populated due to the fact that I am passing a jQuery object and not a string, yet whenever I use items: this.itemsContainer.html(), that simply pastes the html surrounded by quotes… I could simply replace the quotes but that just feels like a hack to me.

Any help would be much appreciated. Thanks!

  • 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-06T18:38:45+00:00Added an answer on June 6, 2026 at 6:38 pm

    jQuery’s append doesn’t take an array:

    .append( content [, content] )

    content: DOM element, HTML string, or jQuery object to insert at the end of each element in the set of matched elements.
    content: One or more additional DOM elements, arrays of elements, HTML strings, or jQuery objects to insert at the end of each element in the set of matched elements.

    If you want to append multiple elements in one call, you have to supply them as separate arguments:

    $(x).append(e1, e2, e3, ...);
    

    so you’d have to use apply to convert your array to separate arguments:

    var $i = $(this.itemsContainer);
    $i.append.apply($i, elements);
    

    That sort of chicanery really isn’t necessary though, you can add them one by one as you create them:

    addItems: function (items) {
        this.items = this.items.concat(items);
        _(items).each(function (item) {
            var attrs = item.attrs || {};
            _.extend(attrs, { 'class': 'menu_item' + (attrs['class'] || '') });
    
            this.itemsContainer.append(this.make('li', attrs, item.title));
        }, this);
    }
    

    Also note that _.each can take a context argument so you don’t need a separate _.bind call. And I’m pretty sure that this.itemsContainer is already a jQuery object so you don’t need to wrap it $() again.

    You might have problems with your render as well:

    render: function () {
        this.$el.html(this.content.render({
            label: this.options.label,
            items: this.itemsContainer
        }));
        this._label = this.$('.menu-label');
        return this;
    }
    

    I suspect that items: this.itemsContainer is going to end stringifying this.itemsContainer, you might have better luck with something like this:

    this.$el.html(this.content.render({ label: this.options.label });
    this.$el.find('some selector').append(this.itemsContainer);
    

    where 'some selector' would, of course, depend on the HTML structure; you’ll have to adjust the template for this as well.


    Your Github link is broken so I don’t know what code you’re adapting. I do know that your use of constructor is non-standard. Why not use the standard initialize?

    constructor / initialize new View([options])

    […] If the view defines an initialize function, it will be called when the view is first created.

    You should probably do it this way:

    app.ui.SelectMenu = Backbone.View.extend({
        // No 'constructor' in here or anywhere...
        initialize: function (options) {
            this.items = [];
            this.content = JST['common-select_button'];
            this.itemsContainer = $('.dropdown-menu', $(this.content.render()));
    
            // Add any items that we may have added to the object params
            if (options.items) {
                this.addItems(options.items);
            }
        },
        //...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.