Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 80
Registered: ‎03-17-2011
My Device: Playbook Tablet
My Carrier: -
Accepted Solution

Global imports and variables

Hi, I;m using views on my new project, and was wondering if in actionscript I can do imports that happen globally to my entire application.

 

I.e..

 

I have MyApplication.as that does:

 

 

import flash.data.SQLConnection;
import views.Home;

--
private var home:Home;
protected var dbConnection:SQLConnection = new SQLConnection;
var dbFile:File = File.applicationDirectory.resolvePath("assets/mydb.db");
dbConnection.open(dbFile);
--

home = new Home();
addChild(home);

 Obviously the code above is all well separated in methods and everything, but I just put it inline so you can see what I'm trying to accomplish.

 

On my home.as, I then would like to be able to use my connection (dbConnection) started on MyApplication.as, and I would also like to import all the necessary libraries that will be using throughout my application. I.e.. import flash.data.SQLStatement;

 

I'm sure I'll have to use it more than once in different views, and if I have to import it everytime, it will become a bit redundant.

 

Could anyone tell me how to make a variable and an import global throughout the application? And "global" the best way to accomplish it?

 

Thanks in advance,

 

 

Marcos Placona
www.placona.co.uk
My Playbook Apps:
Bonjour | Time Traveller | Magic Ball
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Global imports and variables

Actually, the import statements are just a signal to the compiler, which operates on a per-file basis.  Therefore you can't (as far as I know) do anything to avoid the need for imports in any particular file where the imported names will be referenced.  It's not like in some other languages (e.g. Python, or JavaScript when used with various extension libraries) where imports occur at runtime and you can centralize them like you want.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 80
Registered: ‎03-17-2011
My Device: Playbook Tablet
My Carrier: -

Re: Global imports and variables

 


peter9477 wrote:

Actually, the import statements are just a signal to the compiler, which operates on a per-file basis.  Therefore you can't (as far as I know) do anything to avoid the need for imports in any particular file where the imported names will be referenced.  It's not like in some other languages (e.g. Python, or JavaScript when used with various extension libraries) where imports occur at runtime and you can centralize them like you want.


Would that affect performance though?

 

Marcos Placona
www.placona.co.uk
My Playbook Apps:
Bonjour | Time Traveller | Magic Ball
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Global imports and variables

[ Edited ]

EDIT: Wow dude -- i dont know what happened. I think I might have misread your entire post. I assumed you were trying to share data between your "views". I didn't realize you were just trying to minimize your import statements in your classes. Disregard this post :smileyhappy:

 

 


 

 

hey mplacona,

 

to acheive what you want, you should look into "singleton" classes. In AS3 its not a true singleton class, but it acts like one. You basically share an instance of a class throughout your entire application. You can store common variables and configurations in them. In my applications, i store my SQL connection there so anytime i need to access a data item, i just call it through that class. Same applies to sharing data objects throughout using the Object class.

 

for more information on it here is a post that turned me on to it by jtegen:

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Going-back-to-main-page/m-p/71241...

 

and some more posts on it:

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Dispatch-Event-not-working/m-p/81...

 

also another approach you can take is the SharedObject class from AS3. Basically it is used to store data -- similar to SQLite:

 

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html

 

hope that helps. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Global imports and variables

 


mplacona wrote:

Would that affect performance though?


Would what affect performance?  I basically just said you can't do what I think you're trying to do...

 

 

But to answer from a different direction, imports have no impact on runtime performance because, as I said, they are not executed at runtime.  Their effect is limited to what they do during compilation.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Global imports and variables

 


mplacona wrote:
Could anyone tell me how to make a variable and an import global throughout the application? And "global" the best way to accomplish it?

 

By the way, you've basically been asking two unrelated things in your post, though you may not have realized it given how you (possibly) thought imports were working.

 

I've addressed the issue of imports, whereas JRab was giving you one way to deal with the other half of your question, about accessing variables from elsewhere in your app.

 

In addition to using his singleton approach, you can rely on the fact that your main class (what you called your "home.as", I assume) is generally instantiated only a single time in the application.  That makes it practically equivalent to a singleton.  (The term singleton is usually used for a class which you cannot instantiate more than once, I think, but probably is also (mis?-)used for a class which is instantiated only once, as with the main class.)

 

If you want to put these variables in your main class, just make them static variables.  That makes it directly accessible by any other class that can "see" your main class, which is generally everything else in the same package.  Example follows:

 

 

// in main class, e.g. Home.as

public class Home extends Sprite {
    public static var version:String;

    // other vars here, maybe non-public or non-static

    public function Home() {
        Home.version = '1.2.3'; // store global reference

        // initialize app here
    }
}

---------------------------

// in some other module, e.g. Model.as
public class Model {
    
    public function Model() {
        trace('version', Home.version);
    }

 

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 80
Registered: ‎03-17-2011
My Device: Playbook Tablet
My Carrier: -

Re: Global imports and variables

Thanks Peter, that answered my question. :Clap:

Marcos Placona
www.placona.co.uk
My Playbook Apps:
Bonjour | Time Traveller | Magic Ball
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Global imports and variables

[ Edited ]

in addition to peter's answer about using a public static variable, i'd just like to add that communication between objects in AS3 is largely event based.  while in some cases a simple static variable will suffice, it's often more wise to precisely control the data by passing broadcasting it thru a custom event.

 

assuming the objects you want to communicate are not within the same display stack or even of the same class, you can use the global stage or even the Native Applicaiton singleton as the application wide channel to dispatch and listen for your custom events.

 

as for singletons – they're largely frowned upon by OOP purists, but whatever, they can work well and certainly have their place in my opinion.  here is a template you can use to build your own singletons in AS3:

 

 

package
{
//Class
public final class MySingleton
	{
	//Properties
	private static var singleton:MySingleton;

	//Constructor
	public function MySingleton()
		{
        	if	(singleton)
			throw new Error("MySingleton is a singleton that cannot be publically instantiated and is only accessible thru the \"mySingleton\" public property.");
        }

	//Singleton Getter
	public static function get mySingleton():MySingleton
		{
		if	(!singleton)
			singleton = new MySingleton();

		return singleton;
		}
	}
}

 

 


PlayBook Applications:
Drop Swatch