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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:57:03+00:00 2026-06-09T05:57:03+00:00

I have the following Backbone (in coffeescript) view where i pass an array and

  • 0

I have the following Backbone (in coffeescript) view where i pass an array and try to append each item to the el. However, I keep coming up with an empty el.

ItemTagList = Backbone.View.extend
  className  : "itemTagsContainer"
  initialize : (tags) ->
    this.render(tags.taglist)

  render: (taglist) ->
    taglist.forEach (tag) ->
      tag_span = _.template($("#tag").html(), {tag : tag} )
      $(this.el).append(tag_span)
      console.log(tag)

The template $("#tag") looks like this:

<a class='tag' href="/items?tagged_with=<%= tag =>"><%= tag =></a>

Upon rendering, the above will correctly iterate over the taglist, so I know that the taglist is properly passed to the Backbone View, as well as to the render function.

But if I do the following

itemTagList = new ItemTagList
  taglist : ["Tag1", "Tag2"]

console.log(itemTagList.el)

I always get the empty el, i.e.

 <div class="itemTagsContainer"></div>

instead of

 <div class="itemTagsContainer">
   <a class='tag' href="/items?tagged_with=Tag1">Tag1</a>
   <a class='tag' href="/items?tagged_with=Tag2">Tag2</a>
 </div>

which is what I was hoping to get.

Can anyone help?

  • 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-09T05:57:04+00:00Added an answer on June 9, 2026 at 5:57 am

    You just have a context problem inside your forEach callback function; the @ (AKA this) will be the global object (window in a browser) so this.el isn’t what you think it is. You can get around this by using a fat-arrow (=>) when defining your callback function:

    render: (taglist) ->
      taglist.forEach (tag) =>
        tag_span = _.template($("#tag").html(), tag: tag)
        @$el.append(tag_span)
        console.log(tag)
    

    I’ve also switched to @$el since your view will already have a cached version of $(@el).

    The context in a forEach callback depends on what mode you’re in:

    If a thisArg parameter is provided to forEach, it will be used as the this value for each callback invocation as if callback.call(thisArg, element, index, array) was called. If thisArg is undefined or null, the this value within the function depends on whether the function is in strict mode or not (passed value if in strict mode, global object if in non-strict mode).

    I’d recommend using => so that you don’t have to worry about it; alternatively, you could use the thisArg:

    taglist.forEach (tag) -> ...., @
    

    but that tends to be a bit ugly and cumbersome in CoffeeScript.

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

Sidebar

Related Questions

I have the following method on a backbone view defined in coffeescript: saveObservation: =>
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
I have the following views: window.DmnView = Backbone.View.extend({ template: _.template($(#tmpl_dmnListItem).html()), events: { click .getWhois:
I have the following class that extends Backbone.View, I want all my backbone views
I have a backbone app with a view structure that looks like the following
I have the following scenario in a backbone view. I wish to set the
I've been working in backbone.js and came across the following snippet of code. _(view.buttonViews).each(function(button)
I have following setup in my Backbone View: var SomeView; SomeView = Backbone.View.extend({ initialize:
I have the following in my coffeescript/backbone app. The problem is it's running console.log
I'm using Backbone and have the following view: class GT.RunManager.Views.ViolationMarker extends Backbone.View className: 'violation-marker'

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.