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
New Contributor
zeno
Posts: 8
Registered: ‎03-23-2011
My Device: developer
My Carrier: developer

Suggestion on how to save a variable to be used next time the application is launched

Hello,

 

In my JavaScript function I have the following:

 

var randomNumber = Math.floor(Math.random()*225);

 

that I use to display certain values from an xml file, that is part of the application.

 

How could I save the above variable (where to ?) in a pair (date – randomNumber)

 

If the application is launched the same day the variable should be read from this ‘global variable’, if not a new random value generated.

 

So where/how could I save this ‘global variable’ ? Is it possible ?

 

Thank you

Please use plain text.
Developer
ryanmc
Posts: 71
Registered: ‎02-04-2011
My Device: Blackberry Bold & Pearl
My Carrier: Telecom NZ

Re: Suggestion on how to save a variable to be used next time the application is launched

On BOS6 and the playbook you should be able to go

 

localStorage.setItem('itemname','itemvalue');

var item = localStorage.getItem('itemName');

Please use plain text.
BlackBerry Development Advisor (Retired)
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Suggestion on how to save a variable to be used next time the application is launched

/agreed

 

Prior to HTML5 support, storing a value like this in a cookie is probably the easiest way to persist a simple value like a number.

 

 

function setCookie(name, value, days)
{
   var dt = new Date();
   dt.setDate(dt.getDate() + days);
   document.cookie = name + "=" + escape(value) + "; expires=" + dt.toUTCString()
}

 

Cheers,

Adam

 

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
New Contributor
zeno
Posts: 8
Registered: ‎03-23-2011
My Device: developer
My Carrier: developer

Re: Suggestion on how to save a variable to be used next time the application is launched

Thank you for the answers.

 

I had for a Playbook application

 


function start()
{
var dayShow = 0;
var randomDay = Math.floor(Math.random()*225);

var today = new Date();
var d = today.getDate();

localStorage.setItem('today',d);
localStorage.setItem('randomDay',randomDay);

var dateSaved = localStorage.getItem('today');


if(dateSaved != d)
{
   dayShow = randomDay;    
}
else
{
   dayShow = localStorage.getItem('randomDay');    
}

 

.............

 

and it did not work (i.e. dayShow was all the time different so not retrieved).

 

I will try the cookies too.

 

Is there a way to debug in the Playbook simulator ?

 

Thank you

Please use plain text.
Developer
levidehaan
Posts: 49
Registered: ‎02-09-2011
My Device: Not Specified

Re: Suggestion on how to save a variable to be used next time the application is launched

give Amplify a go, www.amplifyjs.com it works on the playbook

and for debugging click on my name and go to the bottom you will see my post on how to debug with firebug

give some kudo love if you find this stuff useful :smileyhappy:

Please use plain text.
Developer
abarber
Posts: 189
Registered: ‎08-13-2008
My Device: Not Specified

Re: Suggestion on how to save a variable to be used next time the application is launched

Hi

 

I've managed to get the local storage working in the playbook.

Have you tried calling the get in a different function from the set?

'

I use these 2 functions

 

function SetLocalStorage(iv_key,iv_value){
    try {
        if (!(localStorage)) {
            alert('No local storage');
        }
        else {
            localStorage.removeItem(iv_key);
            localStorage.setItem(iv_key, iv_value);
        }
    }
    catch(e){
        alert('SetLocalStorage:' + e.message + '.');
    }
}

function GetLocalStorage(iv_key){
    try {
        if (!(localStorage)) {
            alert('no local storage');
        }
        else {
            return localStorage.getItem(iv_key);
        }
    }
    catch(e){
        alert('GetLocalStorage:' + e.message + '.');
    }
}

 

cheers

 

Andrew

Please use plain text.
Developer
levidehaan
Posts: 49
Registered: ‎02-09-2011
My Device: Not Specified

Re: Suggestion on how to save a variable to be used next time the application is launched

or you could just do

amplify.store('storage_name', vartostore);

var cheese = amplify.store('storage_name');

 

there is also a useful pub/sub functionality

amplify.subscribe("store_a_var", function(store_me){

amplify.store('var_to_store', store_me);

});

 

// using jquery

 

$('#someid').live('click', function(){

amplify.publish('store_a_var", data_to_send);

});

 

and some other things useful stuff that Amplify :smileyhappy:

Please use plain text.