blueFast
blueFast

Reputation: 44511

Hook into the .then method of a promise

(js newbie here)

I have overloaded the RESTAdapter:

/*
  The default RESTAdapter in the application.

  An instance of this object will be created automatically.
*/

MyApp.Adapter = DS.RESTAdapter.extend({
    ajax: function(url, type, hash) {
        var ajax_reply = this._super(url, type, hash);
        console.log('ajax_reply (promise) -> '); console.log(ajax_reply);
        return ajax_reply;
    }
});

Now I get the promise which I can see in the console:

promise

I would like to hook into the .then method so that I can show the json data returned by the ajax call, but without interferring in the workings of the RESTAdapter. For example, the RESTAdapter.find method is:

  find: function(store, type, id) {
    var root = this.rootForType(type), adapter = this;

    return this.ajax(this.buildURL(root, id), "GET").
      then(function(json){
        adapter.didFindRecord(store, type, json, id);
    }).then(null, DS.rejectionHandler);
  },

I would like to see in the console all the json replies passed via the .then method. How can I "hook" into the promise?

Upvotes: 0

Views: 285

Answers (2)

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

To log all .ajax() responses, do what @LukeMelia suggests.

To log the response to a particular MyApp.Adapter.ajax() call, try :

MyApp.Adapter.ajax(url, type, hash).then(function(json) {
    console.log(json);
}, function() {
    console.log('.ajax error');
});

Upvotes: 1

Luke Melia
Luke Melia

Reputation: 8389

Something like this should work:

MyApp.Adapter = DS.RESTAdapter.extend({
  ajax: function(url, type, hash) {
    var ajaxPromise = this._super(url, type, hash);
    ajaxPromise.then(function(json){
      console.log(json);
    });
    return ajaxPromise;
  }
});

Upvotes: 2

Related Questions