Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GoogleCodeExporter opened this issue Aug 6, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

[deleted comment]

1 similar comment
@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

Support more types for object members:
 * tuple
 * unicode string

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

@GoogleCodeExporter
Copy link
Author

# 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
Copy link
Author

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

  • Changed state: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant