EmberJS:在处理请求成功时解决承诺

EmberJS: Resolve promise in the success of processing request

我调整了 RESTAdapter 中的 ajax 调用以从 vert.x 总线桥检索数据。但是,我在处理请求的成功部分检索承诺时遇到问题。

我想问题出在 Ember 预期的数据结构形式上。 这是我的适配器代码:

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'
});

这是服务器:

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

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

   return obj;
};

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.floor((Math.random() * 100) + 1);
    }
    mindMaps[mindMap.id] = mindMap;
    responder({
        "data": mindMap
    });
});

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

控制台输出如下:

{"data":[{"type":"mindMaps","name":"One","id":59},{"type":"mindMaps","name":"Two","id":99},{"type":"mindMaps","name":"Three","id":55}]}

WARNING: Encountered "data" in payload, but no model was found for model name "datum" (resolved model name using mind-maps@serializer:-rest:.typeForRoot("data"))

Error while processing route: mind-maps Assertion Failed: The response from a findAll must be an Array, not undefined Error: Assertion Failed: The response from a findAll must be an Array, not undefined

我想我没有 return 以 Ember 期望的形式从服务器获取数据。

想法?

BR, 米兰

除非您还想创建自己的序列化程序,否则您的 JSON 需要采用特定格式。您可以看到记录的格式 here.

如果您无法将您的服务器修改为 return JSON 格式,您需要编写自己的 Serializer, and convert your data into the format listed here.

如果这听起来有点多,那是因为确实如此。我认为 Ember-Data 的适配器和序列化程序是它的头号问题。不幸的是,没有很多简单的方法可以做你想做的事。