Error with returning promise


#1

Hello,

I adapted the ajax call in RESTAdapter for retrieving the data from vert.x bus bridge. But, I have a problem with retrieving the promise in the success part of processing request.

Here is the code of the adapter:

var VertxAdapter = DS.RESTAdapter.extend({
  protocol: 'http',
  host: 'localhost',
  port: '4200',
  addressMapping: {
    "GET localhost/mindMaps": "mindMaps.list"
  },
  pendingRequests: [],

  vertx: function() {
    var adapter = this;

    if (typeof adapter._vertx === 'undefined') {
      adapter._vertx = new vertx.EventBus(adapter.protocol + '://' + adapter.host + ':' + adapter.port + '/eventbus');
      console.log('initialize vertx');
      adapter._vertx.onopen = function() { 
        // TODO: register for messages send from server to this client

        adapter.processPendingRequests.apply(adapter);
      };
    }

    return adapter._vertx;
  },

  ajax: function(url, type, params) {
   console.log(url, type, params);

    var adapter = this;

    var messageAddress = adapter.addressMapping[type + " " + url];

    return new Ember.RSVP.Promise(function(resolve, reject) {
      var success = function(json) {
        Ember.run(null, resolve, json);
      };
      var error = function(json) {
        Ember.run(null, reject, json);
      };

      if(adapter.vertx().readyState === vertx.EventBus.OPEN) {
        console.log('ready state');
        adapter.processRequest(messageAddress, params, success, error);

      } else {
        var pendingReq = {
          address: messageAddress,
          params: params,
          success: success,
          error: error
        };
        console.log('push pending req');
        adapter.pendingRequests.push(pendingReq);

      }

    });

  },

  processPendingRequests: function() {
    console.log('processPendingRequests');
    var adapter = this;
      adapter.pendingRequests.forEach(function(req){
        console.log(req);
          adapter.processRequest(req.address, req.params, req.success, req.error);
      });
  },

  processRequest: function(address, params, success, error) {
    // console.log(address, params, success, error);
    var adapter = this;
    adapter.vertx().send(address, params, function(json){
      //TODO: implement
      //console.log(arguments);
      console.log(JSON.stringify(json));

      success(json);

      //error(json);

    });

    adapter.pendingRequests = [];
  }

});

export default VertxAdapter.extend({
	port: '8080',
	version: 1
});

Here is my server:

var eventBus = require("vertx/event_bus");
var mindMaps = {};

extend = function(that, obj) {
   for (var i in obj) {
      if (obj.hasOwnProperty(i)) {
         that[i] = obj[i];
      }
   }

   return that;
};

eventBus.registerHandler("mindMaps.list", function(args, responder) {
	responder(
	{
		"data": Object.keys(mindMaps).map(function(key) {
			return extend(
				{
					"type": "mindMaps"
				},
				mindMaps[key]
			);
		})
	});
});

eventBus.registerHandler("mindMaps.save", function(mindMap, responder) {
	if (!mindMap.id) {
		mindMap.id = Math.random();
	}
	mindMaps[mindMap.id] = mindMap;
	responder({
		"data": mindMap
	});
});

eventBus.registerHandler("mindMaps.delete", function(args, responder) {
	delete mindMaps[args.id];
	responder({});
});

And the console output:

I don’t know in which form does Ember expect the data structure to be returned. I will use any kind of help. Thank you.

BR, Milan