Using JSDOs to create mobile and web clients : Creating and managing access to a JSDO instance
  

Creating and managing access to a JSDO instance

The following code fragment shows a simple JavaScript coding sequence for creating and using a JSDO referenced by the variable jsdoCustomer to read resource data. The entire fragment executes based on the successful creation and initialization of its login session using the progress.data.getSession( ) stand-alone function, which returns a jQuery Promise:
var serviceURI = "http://oemobiledemo.progress.com/OEMobileDemoServicesForm/",
catalogURI = "http://oemobiledemo.progress.com/OEMobileDemoServicesForm/static/CustomerService.json";

// create a JSDOSession for a specified web application
// that requires Form authentication
try {
// create and initialize login session
progress.data.getSession( {
authenticationModel : progress.data.Session.AUTH_TYPE_FORM,
serviceURI : serviceURI,
catalogURI : catalogURI,
username: "formuser",
password: "formpassword"
})
.then(function( session, result ) { // 1

// Create a JSDO and fill it with data
var jsdoCustomer = new progress.data.JSDO('Customer');
return jsdoCustomer.fill()

}, function(result, info) {
console.log("getSession failed");
})
.then(function (jsdo, result, request) { // 2
console.log("Fill went okay!");
// Do other JSDO related tasks here
}, function (jsdo, result, request) {
console.log("Not filling it today");
// Do JSDO related failure stuff here
})
.always(function () { // 3
// By the time we get here, we should be done with all JSDO related stuff
// and cleaning up
return progress.data.invalidateAllSessions();
})
.then(function( result, info ) { // 4
console.log("All sessions are invalidated!");
}, function() {
console.log("A session failed validation!");
});
} catch (ex) { // 5
console.log("Exception: " + ex.message);
}
The fragment relies on a public Progress web application (serviceURI) that is protected using Form authentication and provides access to the Data Service Catalog (catalogURI) for the CustomerService Data Object Service. Once getSession( ) returns its Promise, all further processing depends on results from a try..catch and a chain of Promise methods invoked as follows:
1. The initial Promise then( ) method callbacks handle the success of the JSDOSession instantiation and login, where a successful resolution first invokes the instantiation of a JSDO instance (jsdoCustomer) for the Customer resource, then invokes the instance's fill( ) to read the resource data and return the method's Promise with the results.
2. The second Promise then( ) method callbacks handle the success of invoking the fill( ) method, where the callback for a successful resolution invokes other JSDO tasks, such as presenting the data for viewing and update. This might also include the creation and initialization of additional login sessions that provide other Data Object Services for access by other JSDO instances.
3. After all processing based on the initial login session is complete (successful or not), the third Promise always( ) method callback invokes the progress.data.invalidateAllSessions( ) stand-alone function to clean up all login sessions that have been created in the chain, which also cleans up the JSDO instances for which they were created.
4. The fourth Promise then( ) method callbacks handle the success of invoking the invalidateAllSessions( ) stand-alone function.
5. As always, the catch block handles any exception thrown in the process of invoking methods, functions, and statements within the try block.
Note: If you know initially what login sessions you want to create and initialize using the getSession( ) stand-alone function, you can start the Promise chain using a jQuery when( ) method: $.when(p1,p2,p3...,px).then({function(){},function(){}})...., where p1,p2,p3...,px are references to the Promises returned from one or more invocations of getSession( ). In this case, the then( ) method success callback executes only if all the getSession( )-returned Promises that you pass to when( ) successfully resolve. Otherwise, your JSDO processing is similar to using a single login session as described in the above example.
For more information on:
*These stand-alone functions — See the descriptions of the getSession( ) stand-alone function and invalidateAllSessions( ) stand-alone function.
*The JSDOSession methods that these functions invoke — See the progress.data.JSDOSession class description.
*Instantiating a JSDO instance and the features it provides — See the progress.data.JSDO class description.
*Implementing JSDO method calls to invoke operations on Data Object resources — See Accessing standard CRUD and Submit operations and Accessing custom Invoke operations.
*Managing login sessions to support JSDO access to network resources — See Managing JSDO login sessions.