Friday, 15 February 2013

android - How to make an object global in Javascript? -



android - How to make an object global in Javascript? -

i made little phonegap/cordova application, , need access object within .js file. turns out have no thought on how alter construction of code create happen.

here index.html code :

<!doctype html> <html> <head> <title>nfc tag id reader</title> <script type="text/javascript" src="js/index.js"></script> <script type="text/javascript" charset="utf-8"> // wait device api libraries load document.addeventlistener("deviceready", ondeviceready, false); // device apis available function ondeviceready() { window.requestfilesystem(localfilesystem.persistent, 0, gotfs, fail); } function gotfs(filesystem) { filesystem.root.getfile("readme.txt", {create: true, exclusive: false}, gotfileentry, fail); } function gotfileentry(fileentry) { fileentry.createwriter(gotfilewriter, fail); } function gotfilewriter(writer) { writer.onwriteend = function(evt) { console.log("contents of file 'some sample text'"); writer.truncate(11); writer.onwriteend = function(evt) { console.log("contents of file 'some sample'"); writer.seek(4); writer.write(" different text"); writer.onwriteend = function(evt){ console.log("contents of file 'some different text'"); } }; }; writer.write("some sample text"); //make object global ? } function fail(error) { console.log(error.code); } </script> </head> <body> <div class="app"> <script type="text/javascript"> app.initialize(); </script> </body> </html>

and here index.js code :

var app = { /* application constructor */ initialize: function() { this.bindevents(); console.log("starting nfc reader app"); }, /* bind events required on startup listeners: */ bindevents: function() { document.addeventlistener('deviceready', this.ondeviceready, false); }, /* runs when device ready user interaction: */ ondeviceready: function() { nfc.addtagdiscoveredlistener( app.onnfc, // tag scanned function (status) { // listener initialized app.displaycpt("<b>"+cpt+"</b>" + ' personnes restantes.'); app.displaybjr("\n"); app.displaybjr("identifiez-vous:"); }, function (error) { // listener fails initialize app.display("nfc reader failed initialize " + json.stringify(error)); } ); }, /* displays tag id @nfcevent in message div: */ onnfc: function(nfcevent) { var tag = nfcevent.tag; var nfcuid = nfc.bytestohexstring(tag.id); var mydb = { "04c85ccab52880": { "name": "name", "firstname": "fname", "societe": "work" } var mapped = object.keys(mydb).map(function(uid){ homecoming (mydb[uid].uid = uid) && mydb[uid]; }); for(var = 0; < mapped.length ; i++){ if(mapped[i]['uid'] != nfcuid){ mapped[i]['uid'] += 1; } else { mapped[i]['uid'] = nfcuid; app.display(mapped[i]['name'] + ' ' + mapped[i]['firstname'] + ', ' + mapped[i]['societe']); writer.write(mapped[i]['name'] + ' ' + mapped[i]['firstname'] + ', ' + mapped[i]['societe']); //i need author usable in order write array content file } } }, }; // end of app

i think writer object needs global in order create mapped array write file, can't find way that.. ideas ?

thanks

have tried putting var writer outside of , removing arguments list of gotfilewriter(writer)? should create global variable. note isn't best of programming practices, 1 should avoid global variables possible.

javascript android cordova

No comments:

Post a Comment