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
ajerman
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Check if variable passed to view is defined

Hey guys, I've got another relatively simple question here that I can't for the life of me find the answer to anywhere.

 

I have a view that let me create a new entry in the DB, or edit the current entry, all based on the date. I want to be able to use the same view to edit older entries, so I'm passing a variable, selectedWeek, to that view through the pushView function. I want to make it use the passed variable if one is passed, and use the current week if nothing is passed.

 

I figured a simple if(data.selectedWeek) would work, but that gives me the classic case of the function just quitting without any error. Is there a different way to check if a variable is defined or not?

 

Thanks again!

Please use plain text.
Developer
ajerman
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Check if variable passed to view is defined

Also tried a != null and != undefined. All simply leave me with a dead view.

Please use plain text.
Developer
Brandon_Appetizer
Posts: 146
Registered: ‎03-27-2011
My Device: Blackberry Playbook
My Carrier: WiFi

Re: Check if variable passed to view is defined

I'm not sure exactly what you mean, but I think you are looking for something like this:

private function pushView (selectedWeek:<type> = null):void
{
if (selectedWeek == null) // none passed, use current
else // selectedWeek passed
}

Then, you can call pushView() to use the current week, or pushView(otherweek) to use a specified week.


Again, I'm not exactly clear if this is what you are trying to do, do respond with some clarification if I've got it wrong.
----------------------------------------
If this post solved your problem, please click the 'Accept as Solution' button.
If you found this post useful, please provide a Kudo. Thanks.

Approved Apps: Celestial Slingshot
Please use plain text.
Developer
ajerman
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Check if variable passed to view is defined

[ Edited ]

Sorry, I guess I should have been more clear.

 

I'm trying to push data to a new view like this (not my own pushView function, the navigator.pushView):

 

 

navigator.pushView(viewEnterTime,selectedWeek);

 

 

And in the viewEnterTime view, I'm trying to see if that variable was passed or not (it would only be passed if I wanted to edit an older entry, otherwise it would use code to get the current week).

 

Here's the exact code as I'd expect it would work:

 

 

if(data.selectedWeek)
{
	curWeek = data.selectedWeek;
}
else
{
	var week:Number = getWeek(curDate);
	if(week < 10)		// If the week is a single digit, add a 0 to keep it in YYYYWW format
		curWeek = curDate.getFullYear().toString() + "0" + week;
	else
		curWeek = curDate.getFullYear().toString() + week;
}

 

Surely I'm not the only person who wants to have a view they can pass a variable to or not depending on if they need it. I guess I COULD change it to ALWAYS use a variable, and pass the current week when I'm not passing an older one, but that's really annoying and shouldn't be necessary.

 

Please use plain text.
New Contributor
turtlehat
Posts: 4
Registered: ‎03-28-2011
My Device: Pearl 8130
My Carrier: Telus

Re: Check if variable passed to view is defined

Would matter what data type data.selectedWeek is.

 

if (Object == null)

if (String == "")

if (isNaN(Number)

if (int == 0)

 

Why not just put a breakpoint on that line and check what the value is in the Variables Inspector?

Please use plain text.
Developer
ajerman
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Check if variable passed to view is defined

[ Edited ]

Would love to debug it, but I've yet to figure out a way to debug. I have to use the simulator since I use qnx components, but I have never figured out how to get the simulator to debug.

 

The variable is a String prior to passing it into navigator.pushView(...), but it's passing into an Object in the function. I've just retested with != "" and != null, neither work. Now I just have this:

 

 

var editWeek:String;
if(data.selectedWeek != null)
	editWeek = data.selectedWeek;
				
var alert:AlertDialog = new AlertDialog();
alert.title = "DEBUG";
alert.message = "test";
alert.modalAlpha = 0.1;
alert.dialogSize = DialogSize.SIZE_SMALL;
alert.addButton("Ok");
alert.show();

 

 

With the alert just to show if it made it past that if. It never does.

Please use plain text.
New Contributor
turtlehat
Posts: 4
Registered: ‎03-28-2011
My Device: Pearl 8130
My Carrier: Telus

Re: Check if variable passed to view is defined

You should be able to write the value of data.selectedWeek to the Console.

 

trace(data.selectedWeek);

 

The Console should open and show you that value.

 

Please use plain text.
Developer
ajerman
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Check if variable passed to view is defined

[ Edited ]

Ugh, I should have just searched for the debug error a long time ago. Just had to change the IP address in the configuration. Okay, so now I have debugging.

 

It's crashing at the trace with the error: "TypeError: Error #1009: Cannot access a property or method of a null object reference."

 

I understand what it's saying. If I don't pass that variable, it doesn't exist, but surely there's a way to see if a variable was passed with the pushView method, no? I guess I could always just pass a null value of that variable if when I'm not trying to edit a week, but it seems like there should be functionality built into the code to allow you to do that without having to pass a null value just because you need a variable sometimes.

Please use plain text.
Developer
berryawesome
Posts: 54
Registered: ‎12-22-2010
My Device: Not Specified

Re: Check if variable passed to view is defined

Hey,

 

Where is the data object being defined? Make sure you are not out of scope. Also, why no assign a default value to the parameter? That way you can test for either the default value or the argument passed in.

 

 

Please use plain text.
Developer
ajerman
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Check if variable passed to view is defined

I don't know that there is a way to assign a default. You would normally open a view with navigator.pushView(viewEnterTime), but if I want a variable to be passed, I'd add it like so: navigator.pushView(viewEnterTime,selectedWeek). Not sure how you'd default that since it's a built in function unless I did an override which is way too much work for what I was expecting to have some method built in to do.

 

It's looking like the easiest solution is to just change all my other calls to that view to navigator.pushView(viewEnterTime,{selectedWeek:null}), that way the object always exists and is null if I don't need to pass something. My worry about this method would be popping a view from above it and going back to that view. i'm not sure how a view initializes when it's done that way, but luckily in my app I would never open another view from the viewEnterTime view, so I never have to worry about popping back to it without a proper null being set.

 

Unless I'm completely misunderstanding something here since it seems like what I'm trying to do would be pretty common.

 

Kudos for the debug help though since that's actually working now.

Please use plain text.