Hey, So i'm trying to move data from a window which calls a webview, to the webview itself.
Maybe i'm going about it the wrong way, because i'm really trying to get the webview to access data from a local database (sqlite).
So here is where i extract the data from the database, and set it as global.
// Opens on the chart view var view = Titanium.UI.createWebView({ url:('../data/local_webview.html') }); var db = Ti.Database.install('data.sqlite','data');//location of DB var rows = db.execute('SELECT name, sales FROM products'); var dataString = ""; var labelString = ""; while (rows.isValidRow()) { labelString += rows.fieldByName('name'); dataString += rows.fieldByName('sales'); rows.next(); if (rows.isValidRow()){ dataString += ';'; labelString += ';'; } }; var dataArray = dataString.split(';'); var labelArray = labelString.split(';'); Ti.App.myData.labelArray = labelArray;//assigning global Ti.App.myData.dataArray = dataArray;//assigning global // // add to parent window // Titanium.UI.currentWindow.add(view);As you can see, i'm extracting the data to a string, and then converting the string to an array. I then put that array in the ti.App namespace (probably not a great idea, but it should work...)
I then access the data in the html file (could be the source of the problem) like this...
window.onload = function () { data = Ti.App.myData.dataArray; labels = Ti.App.myData.labelArray; }
Win7-64Bit , SDK 1.7.2 , Ti Mobile - Android
4 Answers
Accepted Answer
I found you need to fire events back and forth between the webview and the rest of the Appcelerator code. In the html for the webview you need an event listener which listens for events fired from the javascript. You also need a fire event to fire data back. Same set up goes for the javascript. If you need a code example message me and I will send it to you.
Have you tried to use (Titanium) events?
Ok, so the way i went about solving this was to include the js file that was defining the variable. something like:
< script src="../data/file-to-include.js"> </ script >Now i'm free to reference the data in the webview. I'm not sure if i can edit the data from the webview, and that changes will be persistent. Either way, this solves my problem. Well, one of them! :-)
Have you tried
var view = Titanium.UI.createWebView({ url:('../data/local_webview.html'), data:dataArray, label:labelArray });then use
window.onload = function () { data = view.data; labels = view.label; }This is how you pass data between windows, not sure if it works with webviews of not. the view.data may be wrong, it may be another syntax... Worth a try!
Your Answer
Think you can help? Login to answer this question!