|
|
|
The following page was printed from RemoteCentral.com:
Topic: | Library Scope question This thread has 12 replies. Displaying all posts. |
|
Post 1 made on Wednesday May 5, 2010 at 07:34 |
Dominator Long Time Member |
Joined: Posts: | August 2008 33 |
|
|
Hi Folks I recently moved all my code including page scripts to a library The Library is included with an include statement at the activity level Variables defined at the page level need to be qualified. Note there are page functions func_b and func_c that need access to the page level variables as well. Here is an example: Page script: which has a tag of "FUNC_A" var a,b,c,d; func_a (); // call to function func_a Library Script:
CF.page("FUNC_A").func_a = function () { CF.page().a = ....... CF.page().b = ....... CF.page().c = ....... CF.page().d = ....... } You need to qualify the page variables a,b,c,d in the library defintion of the page function func_a . Two questions:
1) Is there a way to avoid having to qualify the variables 2) Does this incur extra processing having to lookup the page in the config file with the CF. Thanks in Advance Dominic
Last edited by Dominator on May 5, 2010 08:06.
|
Dominic |
|
Post 2 made on Wednesday May 5, 2010 at 11:10 |
sWORDs Long Time Member |
Joined: Posts: | November 2006 373 |
|
|
Create an empty object in your library and set it 's variables from the page script. Library: self = this; self.Widgets = {A1:{label: ""},A2:{label: ""}}; [...] self.Widgets.A1.label = "test"; Pagescript: com.mymodule.Widgets={A1:GUI.widget("PNL_A1"),A2:GUI.widget("PNL_A2")}; The lookups only occur on pageload. Lyndel has posted a script to only load them when referenced, which might be better if you don't use all widgets. In my modules I always use all widgets so I'll look them up on page load.
Last edited by sWORDs on May 5, 2010 11:24.
|
|
|
OP | Post 3 made on Wednesday May 5, 2010 at 18:39 |
Dominator Long Time Member |
Joined: Posts: | August 2008 33 |
|
|
Hi sWORDS
Great technique.From what I see in your example it works great for widgets.
How does this apply to variables defined at the page level as in the example I gave?
If it does.....how do you use it? As variables don't have tags associated to them.
Cheers and thanks for the quick reply.
Dominic
|
Dominic |
|
Post 4 made on Wednesday May 5, 2010 at 19:14 |
Lyndel McGee RC Moderator |
Joined: Posts: | August 2001 12,999 |
|
|
Dominic,
You mention:
CF.page("FUNC_A").func_a = function () {
CF.page().a = ....... CF.page().b = ....... CF.page().c = ....... CF.page().d = .......
}
If p is going to be static at time of function declaration and during runtime, I'd use which will introduce a 'closure':
var p = CF.page("FUNC_A"); p.func_a = function () {
p.a = ....... p.b = ....... p.c = ....... p.d = .......
}
If p is going to be static at time of function declaration but dynamic during runtime, I'd cache p to prevent additional lookups:
var p = CF.page("FUNC_A"); p.func_a = function () { var p2=CF.page(); p2.a = ....... p2.b = ....... p2.c = ....... p2.d = .......
}
|
Lyndel McGee Philips Pronto Addict/Beta Tester
|
|
OP | Post 5 made on Wednesday May 5, 2010 at 22:13 |
Dominator Long Time Member |
Joined: Posts: | August 2008 33 |
|
|
Thanks Lyndel
Worked Like a charm
Cheers
Dominic
|
Dominic |
|
Post 6 made on Thursday May 6, 2010 at 06:12 |
sWORDs Long Time Member |
Joined: Posts: | November 2006 373 |
|
|
On May 5, 2010 at 18:39, Dominator said...
Hi sWORDS
Great technique.From what I see in your example it works great for widgets.
How does this apply to variables defined at the page level as in the example I gave?
If it does.....how do you use it? As variables don't have tags associated to them.
Cheers and thanks for the quick reply.
Dominic It works for function or variables aswell, you actually just reassign the object from pagescript and make sure that they are empty in the lib so nothing goes wrong when not setting them in the pagescript: Library: self = this; self.Vars = {var1:"",var2:""}; self.UpdateGUI = function(){}; self.Widgets = {A1:{label: ""},A2:{label: ""}}; [...] self.Vars.var1 = 1; self.Widgets.A1.label = "test"; self.UpdateGUI();
PageScript: com.mymodule.Vars = {var1:value1,var2:value2}; com.mymodule.UpdateGUI = function(){code}; com.mymodule.Widgets={A1:GUI.widget("PNL_A1"),A2:GUI.widget("PNL_A2")}; Another thing you might find handy: com.mymodule.Vars.var1 is the same as com.mymodule.Vars["var1"] so you can do dynamic code without using eval.
Last edited by sWORDs on May 6, 2010 06:29.
|
|
|
Post 7 made on Sunday October 24, 2010 at 06:22 |
Paul Biron Founding Member |
Joined: Posts: | August 2001 142 |
|
|
I'm trying to use this concept, but get an error in the PageScript. The library name which is included at the activity level is: System.include("com.vantagecontrols.Vantage.js");The library has the following title: @title com.vantagecontrolsThe library has an empty function called userExit_A: var self = this; self.userExit_A = function(){};
When, at the page level, I code: com.vantagecontrols.userExit_A = function(){...};I get "Reference error: com is not defined" Also note that I get the same error when coding: com.vantagecontrols.Vantage.userExit_A = function(){...};Anyone has an idea on how to get this to work ? Paul
Last edited by Paul Biron on October 24, 2010 08:06.
|
___________________ Pronto Level II Certified |
|
Post 8 made on Sunday October 24, 2010 at 10:04 |
b00bie Founding Member |
Joined: Posts: | July 2001 396 |
|
|
IIRC, System.include should reference the case-sensitive name of the actual .js or .pjs filename installed on your system in the libraries folder. IIRC, This works sort of like sounds or fonts. I believe that System.include also requires that the library be included (checked) for at least one activity in the system such that the file will be downloaded and available in the Control Panel. So, if you have a set of libraries that you need to access using System.include, simply create an unused activity and check (add check mark) for the libraries you'd like to use with System.include.
Last edited by b00bie on October 24, 2010 10:22.
|
|
Post 9 made on Sunday October 24, 2010 at 10:31 |
Paul Biron Founding Member |
Joined: Posts: | August 2001 142 |
|
|
The library was being loaded properly. The solution lies in establishing the proper namespace within the library itself. I have found some examples and was able to solve the problem. This is what it looks like: 1. In the library // Setup com.Vantage namespace var com; if (!com) { com = {}; } else if (typeof com !== "object") { throw new Error("com already exists and is not an object"); } if (!com.Vantage) { com.Vantage = {}; } else if (typeof com.Vantage !== "object") { throw new Error("com.Vantage already exists and is not an object"); }
...
com.Vantage.userExit_A = function(){}; // definition of the empty function
...
com.Vantage.userExit_A(); // invoke the function (this needs to happen later, after the activity script is run)
2. In the activity script: function userExit_A () { code... } com.Vantage.userExit_A = userExit_A; // replace the empty function with the real one
Last edited by Paul Biron on October 24, 2010 14:52.
|
___________________ Pronto Level II Certified |
|
Post 10 made on Sunday October 24, 2010 at 18:34 |
BluPhenix Long Time Member |
Joined: Posts: | December 2008 371 |
|
|
Yes this is a closure, as Lyndel suggested.
Sure you need to have a com Class before you call it's methods. The title is there only for PEP to know how with which name it should present the library with.
I don't really get it why don't you declare the function in the library. Why declaring it in the activity script and then switch it?
|
|
Post 11 made on Sunday October 24, 2010 at 20:15 |
Paul Biron Founding Member |
Joined: Posts: | August 2001 142 |
|
|
I am using this library in several activities. This is a generic interface with my Vantage system and I use it to communicate via the Vantage InFusion controller with HVAC, Security, Shades and, of course, to control lighting.
In some activities, I want to do some special display processing after the response is received from the Vantage controller and since this processing is really intended for a specific activity, I do not want to burden the main library, hence the concept of user exits specific to an activity. This keeps things clean. When requested, the main functions in the libary will call user exits specific to an activity or even to a single page, for that matter.
Paul
|
___________________ Pronto Level II Certified |
|
Post 12 made on Monday October 25, 2010 at 02:22 |
BluPhenix Long Time Member |
Joined: Posts: | December 2008 371 |
|
|
Ok. Just to let you know, you don't need to declare an empty function in the library and then append something to it in the activity.
In javascript you can add methods to objects as you please, which meand you can add, anywhere in the code a new method or property to an existing object. In this way you can extend the widget() object an other stuff, like:
widget("SOMETHING").hasHumber = 1;
The instance of the object "SOMETHING" will be expanded with the hasNumber value, but only the mentioned object.
So you can omit the userExit_A function in the library.
|
|
Post 13 made on Monday October 25, 2010 at 05:02 |
Paul Biron Founding Member |
Joined: Posts: | August 2001 142 |
|
|
Got it. Works fine.
Thanks.
|
___________________ Pronto Level II Certified |
|
|
Before you can reply to a message... |
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now. |
Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.
|
|