OMC::JavascriptObject Class Reference

Detailed Description

An object that represents a loaded javascript object and its global context.

This object is already loaded in memory and is persistent as long as the Python object exists.

A Simple Example - Creating a Python-JS Interface

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
js_string = """
function frame_next()
{
frame.setCurrent( frame.current()+1 );
}
function frame_last()
{
frame.setCurrent( frame.current()-1 );
}
function frame_set( target_frame )
{
frame.setCurrent( target_frame );
}
function frame_current()
{
return frame.current();
}
"""
frame_helper = js.load_string( js_string )
#Using javascript to change frames and access the frame handler in JS.
#Go to the next frame
frame_helper["frame_next"].call()
print( "Now on frame: %s"%( frame_helper["frame_current"].call() ) )
frame_helper["frame_last"].call()
print( "Now on frame: %s"%( frame_helper["frame_current"].call() ) )
frame_helper["frame_set"].call( [10] )
print( "Now on frame: %s"%( frame_helper["frame_current"].call() ) ) #10



Javascript Object can be Incrementally Extended

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
#Note- the first trace will result in error since the variable is missing.
js_string = """
function testFunction()
{
MessageLog.trace( initiallyMissingVariable );
}
"""
js_obj = js.load_string( js_string )
try:
js_obj["testFunction"].call()
except:
print( "Variable is missing -- as expected." )
print( "Adding missing variable" )
js_obj["initiallyMissingVariable"] = "No longer missing"
js_obj["testFunction"].call()

Public Member Functions

virtual bool contains (const QVariant &propName) const
 Checks the if the object contains a given property at key or at value if the object is an array. More...
 
virtual void remove (const QVariant &propValue) const
 Removes a property from the object at key or at value if the object is an array. For removing at index on arrays, use pop() method instead.
 
virtual QVariant pop (int propIdx) const
 Pops a property from the array object at index. More...
 
virtual void insert (int propIdx, const QVariant &propValue) const
 Inserts a property in the middle of arrays.
 
virtual void clear () const
 Clears the object of any property.
 
virtual void append (const QVariant &propValue) const
 Appends a property to the end of an array.
 
QVariant call (const QVariant &arguments=QVariant(), const QVariant &selfValue=QVariant())
 Calls the javascript function with the provided arguments and an object representing the object bound to the self at the function's execution. More...
 

Public Attributes

QString source
 The source of the javascript object – the file path if loaded from a file, otherwise from memory.
 
QString type
 Identifies the type of this object as a string value.
 

Member Function Documentation

◆ call()

QVariant OMC::JavascriptObject::call ( const QVariant &  arguments = QVariant(),
const QVariant &  selfValue = QVariant() 
)

Calls the javascript function with the provided arguments and an object representing the object bound to the self at the function's execution.

Parameters
arguments- A list of arguments provided to the function being called. If the function is defined with named-arguments, the arguments will use those names.
selfValue- An object that is bound to the function when called. This object is available with the 'this' object in the context of the function.

Calling a function with 'this' object bound

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
js_string = """
function functionWithThisBound()
{
if( this.contextExists )
{
return true;
}
return false;
}
"""
js_obj = js.load_string( js_string )
results = js_obj["functionWithThisBound"].call( [] )
print( "%s : Expecting False -- no context provided."%(results) )
results = js_obj["functionWithThisBound"].call( [], { "contextExists":True } )
print( "%s : Expecting True -- context provided."%(results) )
Returns
Returns the javascript function return value.

◆ contains()

virtual bool OMC::JavascriptObject::contains ( const QVariant &  propName) const
virtual

Checks the if the object contains a given property at key or at value if the object is an array.

Introspecting a JS Object

from ToonBoom import harmony
sess = harmony.session()
js = sess.javascript
js_string = """
var objectExists = "Exists";
"""
js_obj = js.load_string( js_string )
if js_obj.contains( "objectExists" ):
print( "Exists as expected." )
if not js_obj.contains( "objectDoesntExist" ):
print( "Doesn't exist, as expected." )
Returns
True if contains the property.

◆ pop()

virtual QVariant OMC::JavascriptObject::pop ( int  propIdx) const
virtual

Pops a property from the array object at index.

Returns
The popped value.
Inheritance diagram for OMC::JavascriptObject:
Collaboration diagram for OMC::JavascriptObject: