gpt4 book ai didi

javascript - EmberJS : Resolve promise in the success of processing request

转载 作者:行者123 更新时间:2023-11-29 19:29:11 24 4
gpt4 key购买 nike

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

我想问题出在 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

我想我没有以 Ember 期望的形式从服务器返回数据。

想法?

BR,米兰

最佳答案

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

如果您无法修改服务器以返回该 JSON 格式,则需要编写自己的 Serializer , 并将您的数据转换为列出的格式 here .

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

关于javascript - EmberJS : Resolve promise in the success of processing request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com