Skip to Content
Technical Articles
Author's profile photo Chayanin Chanthananond

Handling Multi-Instance of Page or App

Why bother with Multi-Instance

 

Sometimes you have many page instance that have the same criteria ,but handling it in the normal way, it can be handled just 1 instance that is the first instance according to the document

many instance detected in debug tool

many instance detected in debug tool

 

the document I have mentioned above teach you how to handle multi-instance using event , when new app instance started , the START event triggered and set that new app instance to default application instance to be used in running scenario. example code from official document show below

 


MyAppli.step({ stStart: function(ev, sc, st) 
{
  MyAppli.start();
  MyAppli.events.START.on(function(ev) 
  {
     // filter MyAppli instances other than ev.appliInst for the current scenario
     sc.setDefaultInst(ev) ; 
  });	
}});	

 

But what if you want to handle Multi-Instance at the same time. so you have to get all App or Page instance id and handle it with each instance id, just like you use in tester tab in debug tool.

 

tester%20in%20debug%20tool

tester in debug tool

 

I will show you how to get an instance id of the App and Page.

 

First, You can get an App instance from this code

 

myApplication.instances;

 

then you get an object below , this object show you that there are 3 currently running app instance ( 0 not included ). an instance id is in the form of object keys.

 

{
    "0": {
        "ctxType": "ctx.application",
        "name": "myApplication",
        "instance": 0,
        "data": {
            "ctxType": "ctx.dataClass",
            "appliName": "myApplication",
            "appliInst": 0
        }
    },
    "10001": {
        "ctxType": "ctx.application",
        "name": "myApplication",
        "instance": 10001,
        "data": {
            "ctxType": "ctx.dataClass",
            "appliName": "myApplication",
            "appliInst": 10001
        }
    },
    "10003": {
        "ctxType": "ctx.application",
        "name": "myApplication",
        "instance": 10003,
        "data": {
            "ctxType": "ctx.dataClass",
            "appliName": "myApplication",
            "appliInst": 10003
        }
    },
    "10005": {
        "ctxType": "ctx.application",
        "name": "myApplication",
        "instance": 10005,
        "data": {
            "ctxType": "ctx.dataClass",
            "appliName": "myApplication",
            "appliInst": 10005
        }
    },
}

 

in order to get an instance id as an Array you can use Object.keys() function.

 


var instanceKeys = Object.keys(appInst)

// ["0", "10001", "10003", "10005"]

 

now you use instance id to handle each app

 


myApplication[instanceKeys[0]].myPage.myItem.get();

 

Example Use Case

Let’s me explain code snippet below

I create a function subscribe to pGoogle page LOAD event when the new page loaded , the previous page is destroyed.

in WEB technology Application when you open a new tab or window, it’s create a new App instance.

 

Google.pGoogle.events.LOAD.on(
	function(){
		var inst = Object.keys(Google.instances)
			
		var prevID = appKeys[inst.length-2];
		
		if(Google[prevID]){
			Google[prevID].pGoogle.close();	
		}
	}
)

 

Conclusion

When you dealing with several pages that’s exactly the same , you can use this technique to handle.

by using (Application Name).instances to get the list of all Application Instances.

and you use it by passing instance number as an index e.g. myApplication[12345]

 

Further Reading

https://help.sap.com/viewer/8ecea00c1f854fd0a433c4aef5da1ea2/Cloud/en-US/09493df5ef44400b9b1cad8a716b4edb.html

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.