toxtli / cefpython

Automatically exported from code.google.com/p/cefpython
1 stars 0 forks source link

Allow to set python object using JavascriptBindings.SetProperty() #4

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Let's say I have a big class with lots of methods, allow me to bind the whole 
object and all its methods with just one line.

{{{
bindings.SetProperty("myobject", myobject)
}}}

In javascript you will be able to call:

{{{
window.myobject.method1()
window.myobject.method2()
window.myobject.method3()
}}}

Also make properties accessible:

{{{
window.myobject.property1
window.myobjevt.property2
}}}

Setting python object should also be possible using Frame.SetProperty().

We can list all methods/properties of a python object using dir():

{{{
class a:
    d = 1
    e = 2
    def b():
            pass
    def c():
            pass
a1 = a()
dir(a)
}}}

Returns:

{{{
['__doc__', '__module__', 'b', 'c', 'd', 'e']
}}}

All methods and properties that do not start with _ (checking single underscore 
should be enough) are considered public and will be made accessible to 
javascript provided that their types is one of:

 * list
 * bool
 * float
 * int
 * None
 * dict
 * string
 * function
 * instancemethod

Original issue reported on code.google.com by czarek.t...@gmail.com on 15 Jul 2012 at 11:44

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Support more types for object members:
 * tuple
 * unicode string

Original comment by czarek.t...@gmail.com on 11 Sep 2012 at 7:58

GoogleCodeExporter commented 8 years ago
# Here's a prototype

class Tester:
    def __init__(self):
        pass
    def echoMe(self, str):
        return "String: [%s]" % str

def bindClass(bindings, classObject):
    for name in dir(classObject):
        if name[0] == '_':
            continue
        att = getattr(classObject, name)
        class_name = classObject.__class__.__name__
        if hasattr(att, '__call__'):
            bindings.SetFunction("%s_%s" % (class_name, name), att)

obj = Tester()
bindClass(bindings, obj)

"""
Now you can call the method in JS like this:  
  Tester_echoMe("some text")
"""

Original comment by rich...@gmail.com on 14 Sep 2012 at 11:27

GoogleCodeExporter commented 8 years ago
Done, commit: 
http://code.google.com/p/cefpython/source/detail?r=4bf59996b2b6d550e55e97004954b
ea2360c81dc

There is a new method: [JavascriptBindings].SetObject().

This feature will make it into next 0.41 release.

Original comment by czarek.t...@gmail.com on 14 Sep 2012 at 1:26

GoogleCodeExporter commented 8 years ago
Project will move to Github. Find this issue at the new address (soon): 
https://github.com/cztomczak/cefpython/issues/4

Original comment by czarek.t...@gmail.com on 24 Aug 2015 at 6:23