views:

226

answers:

4

For a project I need to develop an app in Adobe AIR, I choose for the HTML/Ajax version. For now the project is quite small, and contains a login part, and a details part.

What I would like to do is on app launch show login.html, and if that was succesfull show/browse-to details.html .

My question is if there is some quick and dirty way to save some of the info I received from the login (such as userid) in a session/application container?

I briefly looked into their BlackBookSafe example, but what I saw there is that although there are multiple pages, they actually keep the blackbooksafe.html open all the time, and load the childpages into the body, thus saving session data in blackbooksafe.html
But in my eyes it creates kind of a mess, and would rather have every page take care of itself (javascript wise), which will make it a bit easier to read.

A: 

I saw a few hacks, like saving data on a file (used in one of Adobe's examples), or putting it in the database, but I didn't find any quick non-persistent data store. I would love to see a key/value dictionary hooked on to the NativeApplication object, where I could add my non-persistent data, but that doesn't exists.

In the end I went for an IFRAME solution, where the parent page stores the non-persistent data.

Gidon
A: 

Since you're using AIR, I assume this is not a web application.

The reason sessions are needed for web applications, of course, is that web pages don't open persistent connections. However, in your case, you have recourse to normal persistent connection resources, which includes database connections that remain open, and include the login information using whatever dbms connectivity solution you are using. For your purposes, this should accomplish what sessions are used for on the web.

Or, you could use a singleton Users class. that remains open for the duration of the appication.


EDIT:

le dorfier
I use AIR for Ajax. And there is now way to create static classes in javascript. I was hoping for some singleton container where I could place my data.With session I didn't mean "web sessions", but "program session" (from program startup to shutdown). Sql was meant to persist data for multiple ...
Gidon
... uses, while my use is only for the current session, when the program closes, the data should be lost. so using SQL or the filesystem seems an overkill.
Gidon
Ah. I would think the javascript Object would be perfect. Just add name-value pairs ala "o.name = 'username'; o.pwd = 'password';" etc.
le dorfier
AIR provides access to the clipboard. You might be able to do something with that; but you'd need to share it with other apps. Or, since you have access to the DOM, you could use a cookie. But if security is an issue, these are accessible, and it's tricky being sure they are emptied when done.
le dorfier
A: 

Why not just create a regular object:

var storage = {};

Usage:

storage.pswd = 'password';
storage.user = 'username';
// Retrieval:
alert(storage.pswd);
J-P
In what scope? All he has is html pages, I would guess running locally the browser.
le dorfier
Indeed wouldn't work in an environment where I "browse" from page to page. I need to attach the storage object to something that doesn't change, such as the NativeApplication object. But I can't.
Gidon
So "my" solution of having a "parent" html page, with an IFRAME is so far the only working solution...
Gidon
A: 

Okay, I realize this thread is OLD but if it helps someone.

My solution feels like a complete hack - shame on Adobe - but it works.

Store the data as JSON strings in the menu data

Menus persist across page loads, and you can assign a custom data to a menu. So far it seems to only like simple data types, like strings or ints. So the solution is to download json2.js (http://json.org) and use it's stringify() and parse() commands to convert your application state into a JSON string then store that in the .data member of a menu.

I stored it in the first menu. in the items list.
Using jQuery, my application code looked like this:


function InitApp() {
    // check before we create the menu for the very first time.
    var menu = air.NativeApplication.nativeApplication.menu;
    var firstMenu = menu.items[0];
    var dataStore = firstMenu.data;
    if (dataStore == null) {
        air.trace("Create Menu Data Store");
        var dataObj = { 
            msg: "Message Object",
            status: 1 
        };
        var dataObjStr = JSON.stringify(dataObj);
        firstMenu.data = dataObjStr;
        dataStore = firstMenu.data;
        air.trace("Created menu data store");
        air.trace(dataObjStr);
    } else {
        var dataStore = firstMenu.data;
        var obj = JSON.parse(dataStore);
        air.trace("Retrieved Menu Data Store");
        air.trace(obj.msg);
    }

    BuildMenu();
    // check after we create our menu
    firstMenu = air.NativeApplication.nativeApplication.menu.items[0];
    firstMenu.data = dataStore; // pass it along.

}

$(document).ready(InitApp);

This data vanishes when the application is closed, so it is only persistent while the app is open.

The real answer would have been for nativeApplication to support the EXACT SAME FEATURE as the menus!

Note: I wrote this on a Mac, so you'd have to adjust the code for using NativeWindow.menu for windows / linux.

sn0gun