Mojo.Cookie

From WebOS101

Jump to: navigation, search

Contents

How to Save and Retrieve Mojo Cookies

// Save Cookie
this.myValue = "my first cookie";
this.myCookie = new Mojo.Model.Cookie('YourCookieName');
this.myCookie.put(this.myValue);
// Get Cookie
this.savedValueCookie = new Mojo.Model.Cookie('YourCookieName');
this.savedValue = this.savedValueCookie.get();
$('YourDivName').innerHTML = this.savedValue;

To save and retrieve an object rather than a single value

// Save Cookie
this.myObject = {val1: 1, val2: '2nd value', val3: 'things'};
this.myCookie = new Mojo.Model.Cookie('YourCookieName');
this.myCookie.put(this.myObject);
// Get Cookie
this.savedObjectCookie = new Mojo.Model.Cookie('YourCookieName');
this.savedObject= this.savedObjectCookie.get();
$('YourDivName').innerHTML = this.savedObject.val2;

Using global variables to save preferences in a cookie

You can create a set of global variables to save your apps preferences. First, in your StageAssistant (or AppAssistant), add a global variable object to the stage-assistant.js (or app-assistant.js) before the StageAssistant constructor, and add a prefs object to the global variable. If you'd like, you can also add some default values for your preferences.

myApp = {};
myApp.prefs ={};
myApp.prefs.pref1 = "this";
myApp.prefs.prefsTwo = "that";
 
function StageAssistant() {
}

In the setup() of your StageAssistant (or AppAssistant), you can then create a global variable and create your cookie. You can also load preferences from the cookie if they already exist.

StageAssistant.prototype.setup = function() {
myApp.prefsCookie = new Mojo.Model.Cookie(Miles.appName);
var args = myApp.prefsCookie.get();
if (args) {
myApp.prefs = args;
}
// do other setup stuff here. Like, for instance pushScene('myScene')
}


When you need to update your preference cookie later, for instance in a preferences scene, you just update the myApp.prefs object with the new values and then put the prefs object into the cookie again.

myApp.prefs.prefsTwo = "some value";
myApp.prefsCookie.put(myApp.prefs);

A Cookie Bakery Example

When I first looked at cookies in webOS, I was confounded by not being able to write a single property to the cookie without completely overwriting the entire cookie. As a novice, it made for very cluttered code as I tried to make sure I always had the current values I needed in my cookie. Here is one solution for the problem.

Stage or App Assistant

var Bakery = {
'aCookie': {
'cookie': new Mojo.Model.Cookie('a_cookie_for_my_app'),
'dough': { // set default property values here
'ingredient1': 'sugar',
'ingredient2': 'flour',
'ingredient3': 'butter',
'ingredient4': 'chocolate chips'
}
},
'bCookie': {
'cookie': new Mojo.Model.Cookie('b_cookie_for_my_app'),
'dough': { // set default property values here
'ingredient1': 'sugar',
'ingredient2': 'flour',
'ingredient3': 'butter',
'ingredient4': 'pecans'
}
},
'bake': function(cookieName) {
Bakery[cookieName].cookie.put(Bakery[cookieName].dough);
},
'loadCookies': function() {
var cookie, properties;
for (cookie in Bakery) {
if (typeof(Bakery[cookie]) === 'object' && Bakery[cookie].cookie) {
properties = Bakery[cookie].cookie.get();
Bakery[cookie].dough = properties;
}
}
}
};

This code is pretty simple and self-explanatory. Put it somewhere where all your scenes will have access to it.

StageAssistant.prototype.setup = function(event) {
Bakery.loadCookies();
...
...
...
};

Load the cookies when the application starts.

propertyChange Event Handler Example

Now you just need to update the particular dough "ingredient" at the right time and bake the cookie as required. For example, you might have a preferences scene which has widgets representing the cookie values in aCookie. This propertyChange event handler will make sure that the cookie is always correct.

StartupAssistant.prototype.updateCookie = function(event) {
Bakery.aCookie.dough[event.target.id] = event.value;
Bakery.bake('aCookie');
};
Personal tools