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

Native Development

Reply
Trusted Contributor
Mitch99
Posts: 102
Registered: ‎05-09-2013
My Device: BB10 Z10
My Carrier: Virgin
Accepted Solution

Access to app's working directory

I am using WebView in QML to display a HTML page that plots data. HTML page uses JavaScript, and resides in the assets directory. The data it uses is created by the app, and stored in an XML format. App is saving it in its working directory. I obtain this directory by retreiving it in C++ and exposing it to QML using a context property.

 

In QML, the path to my data.xml appears as follows:

 

file:///accounts/1000/appdata/.../data/data.xml

 

and that seems to be the correct path. However, I'm having trouble accessing data.xml when I use this path in my JavaScript function that plots the data contained in the xml file. I've tried using an xml data file in the assets directory (where the HTML page resides) and the access and plotting works fine.

 

I'm guessing that the access and the format of the data.xml path is not usable from outside the QML... so the JS doesn't know what to do with it. Does anyone have any idea how to make this work?

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,098
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Access to app's working directory

From QML files path names are relative to the QML file so try that.

However there is no reason that you wouldn't be able to use a full path either so post your code and we'll have a look.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Trusted Contributor
Mitch99
Posts: 102
Registered: ‎05-09-2013
My Device: BB10 Z10
My Carrier: Virgin

Re: Access to app's working directory

[ Edited ]

Thanks for the prompt response! By all means, here it is... The JS code that loads the XML and builds the data array to be graphed is as follows:

 

		function getUrlVars() {
		  var vars = {};
		  var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			vars[key] = value;
		  });
		  return vars;
		};		
		$(function () {
		// load data
			$.ajax({
				type: "GET",
				//url: "data.xml",
				url: getUrlVars()["filePath"],
				
				dataType: "xml",
				success: function (xml) {
		
					 $(xml).find('bloodpressure').each(function () {
		
						var dia = $(this).find('diastolic').text();
						dia = parseInt(dia, 10);
						var sys = $(this).find('systolic').text();
						sys = parseInt(sys, 10);
						var pulse = $(this).find('pulse').text();
						pulse = parseInt(pulse, 10);
						var strDate = $(this).find('date').text();
						var parts = String(strDate).split('-');

						systolicData.push(	[Date.UTC(Number(parts[0]), Number(parts[1])-1, Number(parts[2])), 	sys]);
						diastolicData.push(	[Date.UTC(Number(parts[0]), Number(parts[1])-1, Number(parts[2])), 	dia]);
						pulseData.push(		[Date.UTC(Number(parts[0]), Number(parts[1])-1, Number(parts[2])), 	pulse]);
					});
		
					buildChart();
				}
			});
		});

 Note, in the above code, if I use url: "data.xml", (which is commented out) instead of the line below, where data.xml is stored in the assets folder, everything works fine...

And the QML code with WebView compponent that invokes the HTML page that holds the above code is:

 

                WebView {
                    url: "local:///assets/charts/BPPlot.htm?filePath=" + dirPaths.data + "data.xml"
                    settings.zoomToFitEnabled: true
                    settings.background: Color.Yellow
                    horizontalAlignment: HorizontalAlignment.Fill
                    settings.defaultFontSize: 5
                    settings.minimumFontSize: 5
                    settings.devicePixelRatio: 1.0
                    settings.viewport: {
                        "width": 1280,
                        "height": 500
                    }

 And finally, I use C++ to obtain dirPaths.data: (this is almmost straight from the documentation)

 

    // Retrieve the path to the app's working directory
    QString workingDir = QDir::currentPath();

    // Build the path, add it as a context property, and expose
    // it to QML
    QDeclarativePropertyMap* dirPaths = new QDeclarativePropertyMap;
    dirPaths->insert("data", QVariant(QString("file://" + workingDir + "/data/")));
    qml->setContextProperty("dirPaths", dirPaths);

 

any suggestions would be greatly appreciated!

Please use plain text.
Developer
BBSJdev
Posts: 6,098
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Access to app's working directory

You didn't mention anything about ajax. :smileyhappy:

 

if you use '../../../data' does it work?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Trusted Contributor
Mitch99
Posts: 102
Registered: ‎05-09-2013
My Device: BB10 Z10
My Carrier: Virgin

Re: Access to app's working directory

Well, yes, Ajax ... but does that change anything? It's just a file path string... :No:

 

Ok, so I tried the "../../../data within the JS:

 

url: "../../../data/data.xml",

 

Didn't work...

Please use plain text.
Developer
BBSJdev
Posts: 6,098
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Access to app's working directory

To be honest I don't know, but the fact that it isn't working is confusing and this is the thing that leaps out you as to where the problem may be.

Can you go to Momentics -> Target File System Navigator and check that I've not put too many or too few '..'s in relative to your qml file?

If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Trusted Contributor
Mitch99
Posts: 102
Registered: ‎05-09-2013
My Device: BB10 Z10
My Carrier: Virgin

Re: Access to app's working directory

I already did - there were 4 "../" blocks, not three... :smileyhappy:, if I understand correctly where the home QML location is...

 

Please use plain text.
Trusted Contributor
Mitch99
Posts: 102
Registered: ‎05-09-2013
My Device: BB10 Z10
My Carrier: Virgin

Re: Access to app's working directory

Just a sanity check, where exactly is the QML home directory in the Target File system Navigator?

Please use plain text.
Developer
BBSJdev
Posts: 6,098
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Access to app's working directory

[ Edited ]

It's relative to the QML file you are calling from so if the QML file is in the assets folder it's relative to that.

So if it's in a sub folder you will need one more '..'.

 

[Edit] I've had a glass of wine so it's affecting my ability to count past 4 at the moment. :smileyhappy:


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 6,098
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Access to app's working directory

One more sanity check the xml in data definately has data in right?

You can check this in the Navigator as well.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.