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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:16:23+00:00 2026-05-25T23:16:23+00:00

Given the backbone view function below, what is the correct way of passing this

  • 0

Given the backbone view function below, what is the correct way of passing this (i.e. the current view) to the anonymous function defined in the callbacks?

addSomething: function(e) {
    var newSomething= this.model.somethings.create({
        someProperty: xxx
    }, {
        success: function(m, response) {
            this.doSomething(); //***HERE****
        },
        error: function(m, response) {
            //Error
        }
    });
},

Without and changes, the this in the anon function is set to the window.

I can a set a reference like this:

var thisView = this;

and then just refer to thisView instead of this in the anon function, but this doesn’t seem very elegant. Is there a better way?

  • 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-25T23:16:23+00:00Added an answer on May 25, 2026 at 11:16 pm

    In order to better organize my code and work around this problem, I never put success and error callbacks directly in-line with my calls. I always split them out to their own functions. This let’s me keep the code clean and also use the _.bindAll method to ensure I have the correct context for this.

    SomeView = Backbone.View.extend({
      initialize: function(){
        _.bindAll(this, "createSuccess", "createError");
      },
    
      addSomething: function(e) {
        var newSomething= this.model.somethings.create({someProperty: xxx}, {
          success: this.createSuccess,
          error: this.createError
        });
      },
    
      createSuccess: function(m, response) {
        this.doSomething();
      },
    
      createError: function(m, response) {
        //Error
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var myView = Backbone.View.extend({ tagName: 'div', className: function() { return this.model.isNew() ? 'new' :
App.Views.VideoView = Backbone.View.extend({ initialize: function() { _.bindAll(this, 'render'); this.model = this.options.model; this.render(); }, render:
In backbone.js under the inherits method, the authors does this: var ctor = function()
I’m just starting out with Backbone.js. I’ve subclassed Backbone.Model and Backbone.View : var Message
Given long long int x, y; , I want a function that can compare
Given this class: class Foo { readonly ILog log; public Foo(ILog log) { this.log
Given var o = {}; var p = new Object(); p === o; //false
Given an inorder-traversal list, what's the best way to create a Binary Min/Max Heap?
Given this code: $user_profile = $facebook->api('/me'); echo $user_profile['name']; I see the following error for
Given this: $ids = ''; I just realized that this: $single = $ids ==

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.