View this page "Returning Array to JavaScript Layer"

5 views
Skip to first unread message

Pedro Fraca

unread,
Nov 8, 2007, 6:38:02 AM11/8/07
to JNEXT discussion group

Click on http://groups.google.com/group/jnext/web/returning-array-to-javascript-layer
- or copy & paste it into your browser's address bar if that doesn't
work.

Amnon

unread,
Nov 8, 2007, 9:18:05 AM11/8/07
to JNEXT discussion group
The question doesn't really fit this discussion group. If you want
this for a Web application then you probably can easily acheive what
you need by creating a JNEXT plugin. If its for something else I'd
suggest checking out some ATL group or codeproject.com

Pedro Fraca

unread,
Nov 9, 2007, 3:46:41 AM11/9/07
to JNEXT discussion group
I think this problematic is also in JNEXT, Then if you know any way to
do, please tell me. I want to make a JNEXT plugin and return an object
array.

Amnon David

unread,
Nov 9, 2007, 5:57:25 AM11/9/07
to jn...@googlegroups.com
The existing JNEXT plugins do not use any platform specific code,
which is why Microsoft specific technologies such as ATL and COM are
not used in the plugins. A way to return an array of results can be
viewed in the soure code of the SQLite3 plugin. The idea here is to
return a string which contains a representation of the array and in
the JavaScript code that receives the string, parse it and put it in a
JavaScript array.

Hope this helps,

-Amnon

Pedro Fraca

unread,
Nov 12, 2007, 6:00:23 AM11/12/07
to JNEXT discussion group
Hi, I uderstand you. about platform, sorry to talk with this specific
platform. I viewed the SQLLite, but this example only return static
array elements, such sttring, numbers and more. I need to return
objects, like a collection of independent objects. I can do it, assign
the n element of an array from javascript layer i.e:

var f = new Array();
for (i=0;i<5;i++)
{
f[i] = new Socket()
}

But i want to do it from plugin layer. Only assign a function to an
Array.

I'm working with this problematic. Do you understand me?

Thanks

Amnon David

unread,
Nov 12, 2007, 4:35:24 PM11/12/07
to jn...@googlegroups.com
Pedro Fraca wrote:
> Hi, I uderstand you. about platform, sorry to talk with this specific
> platform. I viewed the SQLLite, but this example only return static
> array elements, such sttring, numbers and more. I need to return
> objects, like a collection of independent objects.
You can always represent the data of any object using a string and pass
that back to JavaScript where it will be parsed back to JavaScript
objects. If, for instance, you have a collection of independent objects,
you can represent these as a collection of JavaScript objects in one
string. As a simplistic example, suppose you have an array of 3
coordinate instances in C++ , where a coordinate is defined as

class Coordinate
{
public:
float x;
float y;
};

you can easily convert these 3 coordinates into a JavaScript code string
of (for example)
"var arCoords = [ [ 1.4, 4.3 ] , [ 2.0, 9.5 ] , [ 7.6, 2.2 ] ];"

then return this string to JavaScript and eval() it - you'll have all
your coordinate objects in the arCoords array. Of course this is just
one method of doing it, but it does the trick.

> I can do it, assign
> the n element of an array from javascript layer i.e:
>
> var f = new Array();
> for (i=0;i<5;i++)
> {
> f[i] = new Socket()
> }
>
> But i want to do it from plugin layer. Only assign a function to an
> Array.
>
> I'm working with this problematic. Do you understand me?
>
>

I'm not sure I understand. Could you give more information on what you
want to achieve ?

-Amnon

Pedro Fraca

unread,
Nov 13, 2007, 3:51:19 AM11/13/07
to JNEXT discussion group
Thanks for the reply Ammon. Im trying to do the next:

For example we have the next c++ object:

class Coordinate
{
public:
float x;
float y;

int add(int f,int t);
int div(int f,int t);
};

I want to return a Java Script Array containing Coordinate Elements,
and access methods from Java Script Layer.

for example

[Java Script Code]

var myArray = getObjectsArray() // This is a plugin function;
alert(myArray[0].add(5,5));

Do you undestand my problematic?

I understand your last reply, is the same that is develop on SQLLite
to return rows.

Thanks for your interest.

Amnon

unread,
Nov 13, 2007, 8:27:53 AM11/13/07
to JNEXT discussion group
you can do what you request, i.e:

var objMyJNEXTClass = new MyJNEXTClass()
var myArray = objMyJNEXTClass.getObjectsArray();
alert(myArray[0].add(5,5));

after you define the following:


///////////////////////////////////////////////////////////////////
// Define the Coordinate class
///////////////////////////////////////////////////////////////////
function Coordinate( fX, fY )
{
var self = this;
self.fX = fX;
self.fY = fY;

self.add = function ( fX, fY )
{
// just an example - no real meaning
return self.fX + fX + self.fY + fY;
}

self.div = function ( fX, fY )
{
// just an example - no real meaning
return (self.fX + fX) / (self.fY + fY);
}
}

///////////////////////////////////////////////////////////////////
// Define the class that can return the coordinate array
///////////////////////////////////////////////////////////////////
function MyJNEXTClass()
{
var self = this;
self.getObjectsArray = function()
{
var strCoordVals = g_JNEXTDispatcher.invoke( self.m_strObjId,
"CoordsRequest", strPath );
var strEval = "arCoordVals = " + strCoordVals;
eval( strEval );
arCoords = self.CoordVals2CoordObjects( arCoordVals );
return arCoords;
}

// convert the data that was returned from the plugin to
JavaScript coordinate objects
self.CoordVals2CoordObjects = function( arCoordVals )
{
if ( arCoordVals == null )
{
return null;
}

arReply = new Array();
for (var i=0; i<arCoordVals.size; i++)
{
var coord = new Coordinate( fX, fY );
arReply.push( coord );
}

return arReply;
}

self.getId = function()
{
return self.m_strObjId;
}

self.init = function()
{
if ( !g_JNEXTDispatcher.require( "MyCoordinateLib" ) ) //
Defined in the C++ code
{
return false;
}

self.m_strObjId =
g_JNEXTDispatcher.createObject( "CoordinateArray" ); // Defined in the
C++ code
if ( self.m_strObjId == "" )
{
alert( "error initializing CoordinateArray" );
return false;
}

g_JNEXTDispatcher.registerEvents( self );
}

self.m_strObjId = "";
self.init();
}

///////////////////////////////////////////////////////////////////


I hope this helps,
-Amnon

Pedro Fraca

unread,
Nov 15, 2007, 9:49:50 AM11/15/07
to JNEXT discussion group
Thanks for the comment. I know this way. I think is good way. I will
use it. Congratulations for the framework. Is very usefull. I have a
Mac Computer, and if i have time I would help you porting the
framework to mac. I hope JNext will be an important framework.

Best, Pedro.

Amnon David

unread,
Nov 15, 2007, 1:31:38 PM11/15/07
to jn...@googlegroups.com

Pedro Fraca wrote:
> Thanks for the comment. I know this way. I think is good way. I will
> use it. Congratulations for the framework. Is very usefull. I have a
> Mac Computer, and if i have time I would help you porting the
> framework to mac. I hope JNext will be an important framework.
>
Thanks for trying out the framework. If you find time to port it to OS/X
it will be a great contribution (I plan to get a Mac, but it will take
some time)

All the best,
Amnon

Reply all
Reply to author
Forward
0 new messages