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:
- var MyModel = can.model.extend({
- findOne: function(params) {
- return $.ajax({
- url: 'myUrl',
- dataType: 'JSON',
- data: params,
- cache: false,
- beforeSend: function() {
- this.requestStartTime = Date.now();
- }
- });
- }
- }, {
- init: function() {
- // bonus if I could pipe the xhr object to models' init so I could set attrs via xhr
- }
- });
-
- var MyControl = can.Control.extend({
- init: function() {
- MyModel.findOne(params)
- .done(function() {
- this.requestStartTime// undefined *** "this" is not the xhr object
- })
- .fail(function() {
- }
- }
- });
-
- new MyControl();