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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:57:10+00:00 2026-06-08T16:57:10+00:00

Is it ok to assign the @template() to an @el like this: someview.js.coffee class

  • 0

Is it ok to assign the @template() to an @el like this:

someview.js.coffee

class SomeView extends Backbone.View
  template: JST["app/templates/sometemplate"]

  events:
    'click' : 'onClick'

  initialize: ->

  render: ->
    @el = @template()

  onClick: ->
    #do something

index.js.coffee

class IndexView extends Backbone.View
  initialize: ->
    @collection.on('reset', @render, this)

  render: ->
    $(@el).html('')
    for m in @collection.models
      view = new App.Views.SomeView(model: m)
      $(@el).append(view.render().el)

sometemplate.jst.hamlc

.someview_template
  .stuff_inside

The situation I’m in, the SomeView class is being wrapped with a div and it’s making me lose the click event I want assigned directly on the ‘.someview_template’ element. The way I can think to solve this is by reseting the @el = @template()… is that possible or the right way to do this?

Is it the case that the @el of Backbone.View should always be the containing div, and the template should be only the elements contained within that div? Seems like it would be more basic if @el is automatically set to the root node found in the @template(). Should I just let the sometemplate.jst.hamlc contain only “.stuff_inside”, then in SomeView set $(@el).addClass(‘someview_template’), that way I can be sure the events are assigned to that element?

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-08T16:57:12+00:00Added an answer on June 8, 2026 at 4:57 pm

    No, that’s not okay for a couple reasons:

    1. You’ll lose the event bindings on @el so none of your events will work.
    2. The cached @$el won’t match @el anymore.

    If you want to replace the @el, use @setElement:

    setElement view.setElement(element)

    If you’d like to apply a Backbone view to a different DOM element, use setElement, which will also create the cached $el reference and move the view’s delegated events from the old element to the new one.

    You probably want something more like this:

    render: ->
      @setElement(@template())
      @
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a class that basically looks like this: class App { public function newTemplate($filename,
My template looks like this: <#assign senti = ${scmr.results[model]}> <#if senti??> <td>${senti} ---- ${senti.sentimentType}</td>
How to assign a local template variable with a string concatenated just like below:
Let's say we have a XAML code like this: <Style TargetType={x:Type ListBoxItem}> <Setter Property=Template>
I'm writing a template class and at one point in my code would like
Is it possible to do template layouts which you can assign to all your
To assign specific value to 1D array I'm using LINQ like so: int[] nums
I'd like to assign 28 different rotation animations to 28 different views, and start
I would like to assign a variable instead of the string in my following
Having class : template<class T> class Link { Link* myParent_; Link* myLeft_; Link* myRight_;

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.