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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:00:20+00:00 2026-06-10T18:00:20+00:00

I have a code in Javascript as : _.each(this.collection.models, function (student) { $(this.el).append(new StudCloneItemView({

  • 0

I have a code in Javascript as :

 _.each(this.collection.models, function (student) {
            $(this.el).append(new StudCloneItemView({ model: student }).el);
        }, this);    

While I am writing This in coffescript as

_.each this.collection.models  , (student) => 
$(@el).append new Item ({ model:student }).el   

which generates

_.each(this.collection.models, function(student) {
      return $(_this.el).append(new Item({
        model: student
      }.el));
    });

Which isn’t desirable as per my requirement . The last segment of “this” element has been missing into the generated javascript . Its very important.

How would I generate The javascript as mentioned on top using the coffeescript I mentioned for _.each ????

Is there anyway to do that ?? Or am I missing any syntax ?

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

    Your JavaScript:

    _.each(this.collection.models, function (student) {
        $(this.el).append(new StudCloneItemView({ model: student }).el);
    }, this);
    

    and what your => CoffeeScript produces:

    var _this = this; // This is somewhere above your _.each in the generated JS
    _.each(this.collection.models, function(student) {
        return $(_this.el).append(new Item({
            model: student
        }.el));
    });
    

    are functionally equivalent. You don’t need the context argument to _.each since the => generates an alias for this (called _this) that is used inside the callback.


    As an aside, Backbone collections have various Underscore methods mixed in so you don’t have to say _.each(@collection.models, ...), you can use each directly on the collection:

    @collection.each (student) =>
        @$el.append(new StudCloneItemView(model: student).el)
    

    I’ve also switched to the pre-built $el that your view already has, there’s no need to build a new jQuery object on each iteration.

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

Sidebar

Related Questions

I have javascript code some thing like this -- var count=3; var pl=new Array(count);
As you may know, when we have this code in Javascript : function getName()
I have this Javascript code I inherited from another developer. I am very new
I have this javascript code : function showMyVideos(data) { var feed = data.feed; var
I have this code: <div id=star-rating> <script type=text/javascript> $(function(){ $('#star-rating form').submit(function(){ $('.test',this).html(''); $('input',this).each(function(){ if(this.checked)
I have javascript code embedded inside a html template file. When I load this
I have the following Javascript code that sorts divs on my page: $(function() {
I have code in javascript which get keycodes of different keys and set it
I have code on javascript: <script>var index=0</script> How can i pass it to htmlhelper
I have JavaScript code which copies the value of input file and paste it

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.