Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1163 from caridy/invoke-with-data
Browse files Browse the repository at this point in the history
Fixes #1159, PR#1163: sending data from client to server and adding rehydration process when using rpc tunnel to update data with the tunnel respond.
  • Loading branch information
caridy committed Jun 11, 2013
2 parents 3dc1893 + b92e17b commit 4173e49
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 131 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Deprecations, Removals

Features
--------
* PR #1163: Rehydration of data from server to client and from client to server. Any data set thru `mojitProxy.data.set()` or `ac.data.set()` will travel back and forward between the client and server runtime to preserve the state of the mojit instance when using the rpc tunnel.

Bug Fixes
---------
* Issue #1159: tunnel request fails if mojito-data-addon is required on the server side controller.

Acknowledgements
----------------
Expand Down
9 changes: 6 additions & 3 deletions lib/app/addons/ac/data.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ YUI.add('mojito-data-addon', function (Y, NAME) {
* @class Data.common
*/
function Addon(command, adapter, ac) {
var Class = (ac.context && ac.context.runtime !== 'client' ? Y.Model.Vanilla : Y.Model);
var data = command.instance.data,
pageData = adapter.page.data,
Class = (ac.context && ac.context.runtime !== 'client' ? Y.Model.Vanilla : Y.Model);

// create data if needed (on the client side, the mojit proxy is responsible)
ac.data = command.instance.data = command.instance.data || new Class();
ac.data = command.instance.data = (data && data._isYUIModel ? data : new Class(data || {}));
// creating pageData if needed
ac.pageData = adapter.page.data = adapter.page.data || new Class();
ac.pageData = adapter.page.data = (pageData && pageData._isYUIModel ? pageData : new Class(pageData || {}));
}

// The trick here is not to define the plugin namespace,
Expand Down
6 changes: 6 additions & 0 deletions lib/app/autoload/tunnel-client.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ YUI.add('mojito-tunnel-client', function(Y, NAME) {
Y.log('rpc success', 'debug', NAME);
try {
resp = Y.JSON.parse(resp.responseText);
if (resp.data && resp.data.data && command.instance && command.instance.data) {
// rehydration process for the data associated to the mojit instance
// TODO: latency compensation here in case the data
// has changed while waiting for the RPC response
command.instance.data.setAttrs(resp.data.data);
}
adapter.done(resp.data.html, resp.data.meta);
} catch (e) {
adapter.error(e.message);
Expand Down
47 changes: 0 additions & 47 deletions lib/app/mojits/TunnelProxy/autoload/store-provider.server.js

This file was deleted.

104 changes: 36 additions & 68 deletions lib/app/mojits/TunnelProxy/controller.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,20 @@
* See the accompanying LICENSE file for terms.
*/


/*jslint anon:true, sloppy:true nomen:true*/
/*jslint anon:true, nomen:true*/
/*global YUI*/

YUI.add('TunnelProxy', function(Y, NAME) {


function makeAdapter(ac) {
var oldAdapter = ac._adapter,
newAdapter;

newAdapter = Y.mix(oldAdapter, {

rpc: {
originalDone: oldAdapter.done,
ac: ac,
buffer: {
data: '',
meta: {}
}
},

_updateBuffer: function(data, meta) {
var buff = this.rpc.buffer;
buff.data = buff.data + data;
buff.meta = Y.mojito.util.metaMerge(buff.meta, meta);
// metaMerge will strip off the view info, but we need that for
// RPC calls, so we put it back
if (meta.view) {
if (buff.meta.view) {
buff.meta.view = Y.mojito.util.metaMerge(
buff.meta.view,
meta.view
);
} else {
buff.meta.view = meta.view;
}
}
},

flush: function(data, meta) {
this._updateBuffer(data, meta);
},

done: function(data, meta) {
var out,
buffer = this.rpc.buffer;
this._updateBuffer(data, meta);
out = {
status: meta.http.code,
data: {
html: buffer.data,
// including the meta data for resolution on the
// client
meta: buffer.meta
}
};
// We need to do this so that the original done method will
// (eventually) be called. If we don't, we'll loop back to
// this method, recursing forever.
this.done = this.rpc.originalDone;
this.rpc.ac.done(out, 'json');
}

}, true);

return newAdapter;
}

'use strict';

Y.namespace('mojito.controllers')[NAME] = {

__call: function(ac) {
// This key is set by the TunnelServer in _handleRpc().
var proxyCommand = ac.params.body('proxyCommand'),
txId = ac.params.body('txId');
txId = ac.params.body('txId'),
newAdapter;

if (!proxyCommand) {
ac.error(
Expand All @@ -88,13 +26,43 @@ YUI.add('TunnelProxy', function(Y, NAME) {
return;
}

// the new adapter that inherit from the original adapter
newAdapter = new Y.mojito.OutputBuffer(txId, function (err, data, meta) {

// HookSystem::StartBlock
Y.mojito.hooks.hook('adapterTunnel', ac._adapter.hook, 'end', this);
// HookSystem::EndBlock

if (err) {
ac.error(err);
return;
}

ac.done({
status: meta.http.code,
data: {
html: data,
// including the meta data for resolution on the client
meta: meta,
// including mojit data to be rehydrated on the client
data: proxyCommand.instance.data
}
}, 'json');
});

// HookSystem::StartBlock
Y.mojito.hooks.hook('adapterTunnel', ac._adapter.hook, 'start', newAdapter);
// HookSystem::EndBlock

newAdapter = Y.mix(newAdapter, ac._adapter);

// dispatch the command as the proxy
ac._dispatch(proxyCommand, makeAdapter(ac));
ac._dispatch(proxyCommand, newAdapter);
}
};

}, '0.1.0', {requires: [
'mojito-http-addon',
'mojito-params-addon',
'mojito-output-buffer',
'mojito-util'
]});
7 changes: 0 additions & 7 deletions lib/app/mojits/TunnelProxy/defaults.json

This file was deleted.

5 changes: 0 additions & 5 deletions lib/app/mojits/TunnelProxy/definition.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"test": "cd tests && node run.js test --browser phantomjs",
"unit": "cd tests && node run.js test -u --browser phantomjs",
"func": "cd tests && node run.js test -f --browser phantomjs",
"lint": "node bin/mojito jslint",
"lint": "node bin/mojito jslint -p",
"docs": "node bin/mojito docs mojito"
},
"homepage": "http://developer.yahoo.com/cocktails/mojito/",
Expand Down

0 comments on commit 4173e49

Please sign in to comment.