Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Original thread:
Post 10 made on Monday September 13, 2010 at 17:03
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Marco,

The parameter to eval() must be a string, not a call to a function which is what you are doing.

DenonTCPClient.execute("ZMOFF\r")

In short, you put common code as a string (Panel label) and then eval() the contents of the panel label in the activity where you need to run the code. You cannot, for example, share a socket or an instance of your DenonTCPClient across multiple activities. Upon entry into each activity, you would need to create an instance of the DenonTCPClient. This could be done by defining your class and putting the definition code into a panel label and then ....

// forward declaration of the class you will be filling out in eval()
var DenonTCPClient;

// load the class definition from the panel label.
CF.activity.eval(CF.widget('DENON_CLASS', 'SCRIPTS', 'DENON_CORE_ACTIVITY').label);

// create an instance using class just loaded.
var myInstance = new DenonTCPClient();
// startup the instance.
myInstance.connect("127.0.0.1",3000);

@Buzz,

Yes, CF.activity().eval() is same as eval() or this.eval() if the statement is called from within the activity script.

And yes, you can do evals from different scopes and it is supported by Spidermonkey and is simply a side-effect of calling a function from within the current scope, Philips does not control this and therefore will never restrict it.

The use of eval() as such is an advanced topic and you really need to know and understand what is going behind the scenes and the ramifications that follow.

CF.page().eval() will evaluate the script in the context of the page scope which is a child of the activity scope. And therefore, variable shadowing could/would occur. Note that the eval() cannot declare any variables and have them persist upon return. However, if a variable is declared prior to execution of eval(), the string that is evaluated CAN modify the variable. That's why the var declaration of DenonTCPClient class above is required prior to calling eval.

However, the last statement evaluated is returned from a call to eval so, you could do something like.

// Example loading a class from a panel label and then instantiating an instance of said class in an activity.

// Panel label text Prontoscript name is 'MY_CLASS' on Page 'SCRIPTS', activity 'ACTIVITY_CLASSES'.
// A simple constructor function.
function MyClass(x)
{
this.text = x;
}
// a prototypical function.
MyClass.prototype.sayHello() = function()
{
System.print("Hello " + this.text);
}
// last statement executed is what is returned.
MyClass;
// End of Panel label.

// Now, in activity script, do the following. eval returns actual class and eliminates need to declare MyClass as forward declaration from previous example.

var renamedClass = CF.activity().eval(CF.widget('MY_CLASS', 'SCRIPTS', 'ACTIVITY_CLASSES').label);
var y = new renamedClass('Lyndel');
y.sayHello();
System.print(y.constructor.toSource());
// above line prints original function source with name === 'MyClass'.
Lyndel McGee
Philips Pronto Addict/Beta Tester


Hosting Services by ipHouse