Cannot set JS object value

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Cannot set JS object value

Postby CephaluS » Mon Nov 04, 2019 11:41 am

Hello. I want to write/read data between C++ and a JS object using the accessor scheme (I've implemented CefV8Accessor following the documentation). This is the code I'm using in OnContextCreated:

Code: Select all
global_obj = context->GetGlobal();
accessor = new MyV8Accesor();
obj = CefV8Value::CreateObject(accessor, NULL);
global_obj->SetValue("myval", obj, V8_PROPERTY_ATTRIBUTE_NONE);

I guess that code is correct in order to initialize the structure.
At this point I display the value of the object in the browser with alert(myval) JS expression, that returns '[object Object]'.

To set the value of the object I've tried the following expression unsuccessfully:

Code: Select all
obj->SetValue("Hello world...", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);

But I'm blatantly failing at this because alert(..) keeps returning '[object Object]'.
I've tried also to use accessor->Set / accessor->Get unsuccessfully as well.

What code do I lack in order to set/get the value of the object?.

Thanks in advance.
CephaluS
Techie
 
Posts: 21
Joined: Sun Jan 06, 2019 5:22 pm

Re: Cannot set JS object value

Postby magreenblatt » Mon Nov 04, 2019 12:05 pm

In your example "myval" is an object. So if you want to trigger the accessor callback you need to use something like "myval.myproperty".
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Cannot set JS object value

Postby CephaluS » Sat Nov 09, 2019 10:52 am

Thanks for your help. I've finally managed to initialize an object that triggers the accessor callbacks in OnContextCreated().

Code: Select all
//init
CefRefPtr<CefV8Accessor> accessor = new GuiCefV8Accesor();
CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(accessor, NULL);
obj->SetValue("myval", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);

// set
obj->SetValue("myval", CefV8Value::CreateString("Hello world..."), V8_PROPERTY_ATTRIBUTE_NONE);

But the object does't exist in the window (JS alert(myval) command reports "Uncaught ReferenceError: myval is not defined")

I can get the frame window object using context->GetGlobal() but I cannot find a way to link my object to it.

Thank you.
CephaluS
Techie
 
Posts: 21
Joined: Sun Jan 06, 2019 5:22 pm

Re: Cannot set JS object value

Postby magreenblatt » Sat Nov 09, 2019 1:54 pm

You need to add your object to the window object and then use alert(window.myval.myproperty) where myval is your object.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Cannot set JS object value

Postby CephaluS » Sat Nov 09, 2019 2:18 pm

Marhsall, thank you for your quick reply. Adding or linking an object to another object is precisely what I need. I've been reading for hours but I cannot find the way to do it in the documentation or examples.

I've tried this (look at the last line, in my attempt to add and object to another object) but it doesn't work:

Code: Select all
global_obj = context->GetGlobal();

CefRefPtr<CefV8Accessor> accessor = new MyV8Accesor();
CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(accessor, NULL);
obj->SetValue("myproperty", V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("myproperty", CefV8Value::CreateString("Hello world..."), V8_PROPERTY_ATTRIBUTE_NONE);
   
global_obj->SetValue("myval", obj, V8_PROPERTY_ATTRIBUTE_NONE);

Thank you.
Last edited by CephaluS on Sat Nov 09, 2019 2:35 pm, edited 1 time in total.
CephaluS
Techie
 
Posts: 21
Joined: Sun Jan 06, 2019 5:22 pm

Re: Cannot set JS object value

Postby Czarek » Sat Nov 09, 2019 2:34 pm

You should check return values of any functions you call to see if they executed successfully. This includes CreateObject and SetValue.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Cannot set JS object value

Postby CephaluS » Sat Nov 09, 2019 2:47 pm

Hi Czarek, Thanks for your hint... actually that last line worked. I just had to change label id in MyV8Accesor code.

Thanks Marshall and you for your patience and amazing job and help! :)
CephaluS
Techie
 
Posts: 21
Joined: Sun Jan 06, 2019 5:22 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 44 guests