title: Variable scope in Backbone.js
body: <p>I have web application in Backbone.js that is using Stripe payment system.</p>
  
  <p>This is part of the model code:</p>
  
  <pre><code>App.Models.Payment = Backbone.Model.extend({
  
  defaults: {
      id:null
  },
  
  url: function() {
      // code
  },
  
  initialize:function(){      
      this.id='';
      this.saveClicked= false;
      this.saveDetailsChecked= true;
  }
  </code></pre>
  
  <p>this model is used in this view:</p>
  
  <pre><code>App.Views.BillingView = Backbone.View.extend({
  
  el: '#billing-fields',
  
  events: {
      'click #billing-footer-buttons .navigation .confirm-btn': 'saveCardClicked'
  },
  
  initialize:function(models, options) {
      Stripe.setPublishableKey('**********************');
      var self = this;
  },
  
  saveCardClicked:function(e) {
      e.preventDefault(); 
      if (this.model.saveClicked) return false;
      this.model.saveClicked = true;
      var $form = $('#payment-form');
      Stripe.createToken($form, this.stripeResponseHandler);
  },
  
  cancelClicked:function() {
      vent.trigger('showScreen', 'subscribe-container');
  },
  
  stripeResponseHandler:function(status, response) {
      var $form = $('#payment-form');
      self.saveDetailsChecked = document.getElementById("billing-checkbox").checked;
      var atr1 = document.getElementById("billing-card-number").value;
      var atr2 = self.model.savedCard;
      if(document.getElementById("billing-card-number").value == self.model.savedCard){
          self.model.set('use_saved_card','1');
          vent.trigger('doPayment');
      }else{
          if (response.error) {
              // code
          } else {
              // code
          }
      }
      self.saveClicked = false;
  }
  });
  </code></pre>
  
  <p>In the saveCardClicked function I am able to access the variables from the model like the saveClicked variable. </p>
  
  <p>But in the stripeResponseHandler I am not able to access the 'saveClicked' variable from the model, in this function this refers to window, and self variable that is defined in the initialize function cannot be accessed also.</p>
  
  <p>stripeResponseHandler is called from the Stripe API.</p>
  
  <p>Is there any way that I can access the savedCard variable in the stripeResponseHandler function or I should use global variable?</p>