RESTAdapter: promise and Ember.run explanation


#1

I don’t want to put this on stackoverflow because I know they don’t like these types of questions. I am developing a JSONRPC adapter I was wondering if someone would explain how the RESTAdapter works when it comes to returning the promise (with that string), and calling Ember.run. I know the purpose of the run loop, and I already made an adapter that can create or get records and it all works fine, but apparently using the run loop is more ‘proper’ and my boss (who also hasn’t worked with ember but just researched a lot) wants us to use it.

Basically here is my current adapter’s ajax function:

ajax: function(method, args, kwargs, model, url, success, error){ var data = JSON.stringify(this.getData(model, method, args, kwargs, success, error));
var settings = this.getSettings(data, success, error);
var ret = $.ajax(this.get(‘host’)+’/’+this.get(‘namespace’), settings); },

The success and error callbacks are defined in the getSettings function.

Notice I just return a JQ ajax function, while in the RESTAdapter it’s wrapped into a promise:

ajax: function(url, type, options) {
    var adapter = this;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      var hash = adapter.ajaxOptions(url, type, options);

      hash.success = function(json, textStatus, jqXHR) {
        json = adapter.ajaxSuccess(jqXHR, json);
        if (json instanceof InvalidError) {
          Ember.run(null, reject, json);
        } else {
          Ember.run(null, resolve, json);
        }
      };

      hash.error = function(jqXHR, textStatus, errorThrown) {
        Ember.run(null, reject, adapter.ajaxError(jqXHR, jqXHR.responseText));
      };

      Ember.$.ajax(hash);
    }, "DS: RESTAdapter#ajax " + type + " to " + url);
  },

So, my questions are:

  1. What is this string: "DS: RESTAdapter#ajax " + type + " to " + url);
  2. Since my ajax does not return a promise, why use it?
  3. What does Ember.run do in this case? I also dont use that and any record I create or look for, when returned from the server, gets properly loaded into the store.

I can change my adapter and simply use these methods, but I would rather know beforehand how and why REST is written how it is and what it accomplishes that my code does not.

Sorry if you dont like these types of questions here also, I’ll just plead ignorance this first time. Thanks