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

Web and WebWorks Development

Reply
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: Error blackberry is not defined

[ Edited ]

Hi there,

 

Just so we have a common starting point, this is the minimum I required to get the sample working error free in Ripple.

 

 

*** config.xml ***

 

<?xml version="1.0" encoding="UTF-8"?>

<widget xmlns="http://www.w3.org/ns/widgets"
        xmlns:rim="http://www.blackberry.com/ns/widgets"
        version="1.0.0.0"
        xml:lang="en">

    <name>Music Files</name>
    <author>Oros</author>
    <content src="index.html"/>

    <feature id="blackberry.io.dir" />
</widget>

Without the <feature> element, Ripple provides an error saying that appDirs is not defined, and rightly so. If this sample were be run on a physical device, I would also require the following <rim:smileytongue:ermissions>.

 

    <rim:permissions>
        <rim:permit>access_shared</rim:permit>
    </rim:permissions>

This would need to be added inbetween the <widget></widget> tags.

 

 

*** index.html ***

 

<!DOCTYPE html>
<html>
	<head></head>
	<body>
		<div id="log"></div>
		<script type="text/javascript">
			/*global blackberry */

			function log(text) {
				var div = document.createElement('div');
				div.innerHTML = text;
				document.querySelector('#log').appendChild(div);
			}

			window.addEventListener('DOMContentLoaded', function () {
				var dirs, subfolder, musicDir;

				if (typeof blackberry !== 'undefined') {
					dirs = blackberry.io.dir.appDirs;
					log(
						'dirs.app.storage.path: '
						+ dirs.app.storage.path
					);

					log('<br />');
					for (subfolder in dirs.shared) {
						log(
							'dirs.shared.'
							+ subfolder + '.path: '
							+ eval('dirs.shared.'
							+ subfolder + '.path')
						);
					}
					log('<br />');

					musicDir = dirs.shared.music.path;
					log('Music folder path: ' + musicDir);
				}
			}, false);
		</script>
	</body>
</html>

For starters, a <div> was created to keep a running log of output (instead of using alerts; to accomodate this, the log function was also created that appends additional <div> elements to the log.

 

I then added a listener for DOMContentLoaded to ensure that our DOM is fully ready before trying to execute any JavaScript on it.

 

Within Ripple, there are two loads that tend to happen. The first is when the application gets loaded into the browser, and the second when Ripple takes over and actually loads the content in the Ripple container. There may be a better way to explain that, but that's sort of the gist of it.

 

That being said, I've included a check for:

if (typeof blackberry !== 'undefined')

 

The purpose for this is to avoid an error on the 1st load, before Ripple has been loaded, and blackberry isn't defined. On a real device this wouldn't be needed, it's just a quirk of working with Ripple. Even without the if statement, everything will continue to load just fine, but I prefer to avoid error logs where possible.

 

The remainder of the code is based on the sample here:

https://developer.blackberry.com/html5/apis/blackberry.io.dir.html#.appDirs^2

 

Specifically, alerts have been replaced with logs, and some reformatting has also occurred. But beyond that, the functionality is the same.

 

The end result for me in Ripple is the following output:

 

out.png

With Web Inspector reporting the following:

 

out.png

 


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
laurentC
Posts: 268
Registered: ‎02-05-2010
My Device: Blackberry Z10

Re: Error blackberry is not defined

Thanks a lot for your help. You gave me a good starting point to solve my issue.

I'm not sure why but when Sencha Touch launch my app, blackberry is not defined. Certainly because the weborks.js file has not been loaded yet...

 

How I solved this :

 

function initDirs() {

  try {
    dirs = blackberry.io.dir.appDirs;
    musicDir = dirs.shared.music.path;
  }
  catch(e) {
    Ext.defer(function() {
      initDirs();
   }, 1000, this);
}

initDirs();

 

With this workaround, I'm waiting until blackberry is not undefined... 

Twitter : @LaurentKP
My apps
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: Error blackberry is not defined

Glad you managed to find a workaround. I did some more searching and found this in the Sencha forums:

http://www.sencha.com/forum/showthread.php?125539-Need-an-event-to-check-all-sencha-files-are-loaded

 

Are you by chance already waiting for the onReady function before executing your main JavaScript core? (i.e. initializaiton, WebWorks calls, etc.) I would expect that waiting on that function to trigger should mean everything has been loaded.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C

Re: Error blackberry is not defined

I had a similar issue recently

 

Note JQuery...

 

Try this

 

var bb = false;

 

$(document).ready(function() {
    if(typeof blackberry == 'undefined') {
        bb = false;
        } else {
        bb = true;
        }
    if(bb) {
        alert('Blackberry found');
        } else {
        alert('Blackberry not found');
        }   
    });

I ain't tried this on my DevAlpha yet but it should be fine




Click the like button if you find my posts useful!