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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:54:46+00:00 2026-05-30T21:54:46+00:00

I have a view called DashboardView that instantiates multiple WidgetView s. Each widget needs

  • 0

I have a view called DashboardView that instantiates multiple WidgetViews. Each widget needs to have its own event bindings. So far as I can tell, these bindings get lost when the view is rendered and added to the parent view, i.e.:

class DashboardView extends Backbone.View
  constructor: -> 
    context = @
    _.each @collection, (w)->
      dv = new app.WidgetView(model: w)
      context.$el.append(dv.render()) 

class WidgetView extends Backbone.View
  events: 
     "click .config" : "config_widget"

  render: ->
      _.template($("#widget-template").html(), @model)

Doing it this way, the click events on the .config element of the widget are now lost. Is there a better way of mixing the nested views into the parent while ensuring that the event handlers on the child view are channelled correctly?

One solution I have seen to this problem comes in this article. This looks about right, but I’m curious if there is a more elegant way of solving 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-05-30T21:54:48+00:00Added an answer on May 30, 2026 at 9:54 pm

    Try this:

    class DashboardView extends Backbone.View
      constructor: -> 
        @collection.each ( w ) =>
          dv = new app.WidgetView( model: w )
          @$el.append dv.render().el // Append widget's @el explicitly
    
    class WidgetView extends Backbone.View
      tagName: 'div' // or whatever your view's root element is
    
      template: _.template $( "#widget-template" ).html() // pre-compile template
    
      events: 
        "click .config": "config_widget"
    
      render: ->
        @$el.html @template @model.toJSON() // append template to @el
        return this // return view
    

    So, the idea is this:

    (1) Inside the WidgetView‘s render method, you populate @el (the root element of the view) with its model’s data via the template. (And notice how I compile the template only once – there is no need to compile the template on each render operation.)

    (2) Inside the DashboardView, you append the widget’s root element – @el – to the DOM.

    The thing is that the view’s events delegate to its root element – @el. Therefore, you want to work with the root element explicitly: inside render, you populate it, and then you append it to the DOM.

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

Sidebar

Related Questions

I have a view called MyViewController, that I initialize from a xib file. Its
I have a View Controller calling a method in its own view called closeMenu.
We have a View (call it X) that is the base view called by
General context : MVVM application. I have a View called JobView. Its DataContext is
I have a partial view called Login.ascx that has my login boxes that I
I have a view controller class called PresidentsViewController that sets up data in a
I have a view called gallery that options. I want to listen and act
I have a view called Associations that has a drop down list and a
Let's say I have a page view called PagePreview.aspx . on that page I've
I have a Django view called change_priority. I'm posting a request to this view

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.