От: | Serginio1 | https://habrahabr.ru/users/serginio1/topics/ | |
Дата: | 04.01.17 13:50 | ||
Оценка: |
Remote objects
Proxies allow you to create virtual objects that represent remote or persisted objects. To demonstrate this, let's write a wrapper around an existing library for remote object communication in Javascript. Tyler Close's web_send library can be used to construct remote references to server-side objects. One can then "invoke" methods on this remote reference via HTTP POST requests. Unfortunately, remote references cannot be used as objects themselves. That is, to send a message to a remote reference ref, one writes:
Q.post(ref, 'foo', [a,b,c]);
It would be a lot more natural if one could instead treat ref as if it were an object, such that one could call its methods as ref.foo(a,b,c). Using the Proxy API, we can write a wrapper for remote web_send objects to translate property access into Q.post calls:
function Obj(ref) {
return Proxy.create({
get: function(rcvr, name) {
return function() {
var args = Array.prototype.slice.call(arguments);
return Q.post(ref, name, args);
};
}
});
}