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.
I suppose that the problem is with form of data structure that Ember expects. This is the code of my 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'
});
And this is the server:
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({});
});
Here is the console output:
{"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
I suppose that I don't return the data from the server in a form that Ember expects me to.
Ideas?
BR, Milan
Unless you also want to create your own serializer, your JSON needs to be in a specific format. You can see the format documented here .
If you're not able to modify your server to return that JSON format, you'll need to write your own Serializer , and convert your data into the format listed here .
If this sounds a little bit much, that's because it is. I think Ember-Data's adapters and serializers is its number one issue. Unfortunately, there aren't many easy ways to do what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.