diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-10-16 13:37:22 +0200 |
---|---|---|
committer | Milian Wolff <milian.wolff@kdab.com> | 2014-12-02 14:16:55 +0100 |
commit | 81dac6e848da8e8a071a2069862590889a287067 (patch) | |
tree | b8bbca884fd5e04755f6feaa7310e2d02c5fac34 /src/webchannel/qwebchannel.js | |
parent | 86d77a900852691267f556fbde98406a12ee4310 (diff) | |
download | qtwebchannel-81dac6e848da8e8a071a2069862590889a287067.tar.gz |
Make objects inside properties accessible.
Similar to the support for factory-methods, we must wrap objects in
properties to make them accessible to clients. This patch adds the
required code for that. Besides support for simple properties that
reference an object, this patch also adds support for list properties
that contain objects.
The client-side unwrap of properties is delayed until all objects
are initialized, as a property might reference another registered
object.
Change-Id: I9fb90a8eab4c66d2f4231fdb482e0d97d128df3e
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Diffstat (limited to 'src/webchannel/qwebchannel.js')
-rw-r--r-- | src/webchannel/qwebchannel.js | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js index d2c6525..472330e 100644 --- a/src/webchannel/qwebchannel.js +++ b/src/webchannel/qwebchannel.js @@ -161,6 +161,10 @@ var QWebChannel = function(transport, initCallback) var data = message.data[objectName]; var object = new QObject(objectName, data, channel); } + // now unwrap properties, which might reference other registered objects + for (var objectName in channel.objects) { + channel.objects[objectName].unwrapProperties(); + } if (initCallback) { initCallback(channel); } @@ -190,18 +194,31 @@ function QObject(name, data, webChannel) // ---------------------------------------------------------------------- - function unwrapQObject( response ) + this.unwrapQObject = function(response) { + if (response instanceof Array) { + // support list of objects + var ret = new Array(response.length); + for (var i = 0; i < response.length; ++i) { + ret[i] = object.unwrapQObject(response[i]); + } + return ret; + } if (!response || !response["__QObject*__"] - || response["id"] === undefined - || response["data"] === undefined) { + || response["id"] === undefined) { return response; } + var objectId = response.id; if (webChannel.objects[objectId]) return webChannel.objects[objectId]; + if (!response.data) { + console.error("Cannot unwrap unknown QObject " + objectId + " without data."); + return; + } + var qObject = new QObject( objectId, response.data, webChannel ); qObject.destroyed.connect(function() { if (webChannel.objects[objectId] === qObject) { @@ -219,9 +236,18 @@ function QObject(name, data, webChannel) } } }); + // here we are already initialized, and thus must directly unwrap the properties + qObject.unwrapProperties(); return qObject; } + this.unwrapProperties = function() + { + for (var propertyIdx in object.__propertyCache__) { + object.__propertyCache__[propertyIdx] = object.unwrapQObject(object.__propertyCache__[propertyIdx]); + } + } + function addSignal(signalData, isPropertyNotifySignal) { var signalName = signalData[0]; @@ -324,7 +350,7 @@ function QObject(name, data, webChannel) "args": args }, function(response) { if (response !== undefined) { - var result = unwrapQObject(response); + var result = object.unwrapQObject(response); if (callback) { (callback)(result); } @@ -339,6 +365,8 @@ function QObject(name, data, webChannel) var propertyName = propertyInfo[1]; var notifySignalData = propertyInfo[2]; // initialize property cache with current value + // NOTE: if this is an object, it is not directly unwrapped as it might + // reference other QObject that we do not know yet object.__propertyCache__[propertyIndex] = propertyInfo[3]; if (notifySignalData) { |