Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Access xhr object in findOne's success callback or model init?

$
0
0
Normally when I do a jQuery ajax request I can use a beforeSend function to set this.something on the xhr object for that specific request.  So when I get the callback I can have access to this.something, which may be a unique value i.e. timestamp, id etc... Using can.Model I overwrite findOne and simply return an ajax deferred, but the callback on that deferred has a context for a deferred it seems, this != the request's xhr.  Is there an easy way to get this ... "this"?  I've been trying to reverse engineer can.Model but hoping for a quicker answer here.  Just to help explain what I'm saying here's an example:

  1. var MyModel = can.model.extend({
  2.       findOne: function(params) {
  3.             return $.ajax({
  4.                   url: 'myUrl',
  5.                   dataType: 'JSON',
  6.                  data: params,
  7.                  cache: false,
  8.                  beforeSend: function() {
  9.                        this.requestStartTime = Date.now();
  10.                  }
  11.             });
  12.       }
  13. }, {
  14.       init: function() {
  15.             // bonus if I could pipe the xhr object to models' init so I could set attrs via xhr
  16.       }
  17. });

  18. var MyControl = can.Control.extend({
  19.       init: function() {
  20.             MyModel.findOne(params)
  21.                   .done(function() {
  22.                         this.requestStartTime// undefined *** "this" is not the xhr object
  23.                   })
  24.                   .fail(function() {
  25.                   }
  26.       }
  27. });

  28. new MyControl();


Viewing all articles
Browse latest Browse all 3491

Trending Articles