sgzwiz / brython

Automatically exported from code.google.com/p/brython
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

"native" javascript objects are currently not usable #57

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Several calls to the Javascritp API 
(such as doc.getElementsByName, jsobject.style and many others) return
"raw" javascript objects which, as far as I can perceive can't
have their attributes manipulated directly from brython code - as they
lack any "dunder"  ("__<special>__") methods.

I don't know if there is an workaround for that - or a roadmap
on how to overcome this. (A function  call on an object could 
add the missing methods, so that it would work - and that could
be the desired state for example)

I am willing to help overcome this issue.

In the meantime, I wrote this small patch to getattr, so that
attributes on javascript objects can be explicitly obtained:

diff --git a/py_classes.js b/py_classes.js
index 343112f..b17e0c8 100644
--- a/py_classes.js
+++ b/py_classes.js
@@ -355,6 +355,10 @@ function getattr(obj,attr,_default){
         obj.__getattr__(attr)!==undefined){
             return obj.__getattr__(attr)
     }
+    // Allows retrieving attributes of unproxied javascript objects
+    else if (obj[attr] !==undefined){
+        return obj[attr]
+    }

---
This is consistent with the current behavior of "setattr" which
will just write the attribute to the underlying javascript object,
without looking for a "__setattr__" method  (actually this would be another 
issue)

Original issue reported on code.google.com by gwid...@gmail.com on 27 Jan 2013 at 2:54

GoogleCodeExporter commented 9 years ago
Hi,

There was a long discussion about using Javascript objects in Brython on the 
French forum. The solution that arose was a built-in function (currently called 
JSObject, but the name may change) transforming a Javascript object into one 
that can be used by Brython, ie with the __ methods
In your example you would use : 
obj = JSObject(js_obj)

getElementsByName is currently not implemented, but you can do
x=JSObject(doc.getElementsByName('x'))
alert(x.length)

I leave the issue open, please reply if JSObject doesn't work as you expect. 
The feature needs to be documented

Original comment by pierre.q...@gmail.com on 28 Jan 2013 at 9:05

GoogleCodeExporter commented 9 years ago
I will let you know if that works - while thinking on the issue, I had came 
with this approach as a good solution. 
If that works for me, I will add a patch for the English documentation here. 
(sorry, can't help with French)

Original comment by gwid...@gmail.com on 28 Jan 2013 at 9:35