Scope is a nucleus variable, which tells the nucleus, till when a component will exist. We discussed in previous sections, that nucleus properties are preceded by "$" sign in the configuration files.
$scope is the nucleus property which sets the scope of a component. There are many possible values of $scope, which have different behavior. We will discuss each of them soon.
Please note that though $scope is a nucleus property (and takes only certain specific values), it can be set using "=" or ":"; just like any other property.
GLOBAL SCOPE: ( $scope = global )
If a component has global scope, its instance is shared by users across all the sessions that exist for the application.
Any user which accesses the application can change the values of a global scoped component.
Usage example:-
$class=com.mypackage.Person name=Monis age=22OR
$class=com.mypackage.Person $scope=global name=Monis age=22
NOTE: If we do not specify $scope variable, and it is not specified in any of the upper-layers of the component, nucleus considers is scope as "global" by default.
Also, as explained in layering concepts in previous article, if the $scope variable is specified in any of the upper-layers of the component, and is not mentioned in configuration file in our module's config-path, the $scope value is derived from the configuration file in the upper-layer.
SESSION SCOPE ( $scope = session )
For a component scoped as session, a separate instance is provided for each session (or user). ATG uses cookies and re-written URLs to identify requests originating from a single browser session. If the session is inactive (requests are not made) for a specific period of time, the session scoped components are removed from that session, and any runtime values of those components are lost.
Usage Example:-
$class=com.mypackage.Person $scope=session name=Monis age=22
REQUEST SCOPE ( $scope = request )
As the name suggests, for each request for a component, a new instance will be generated for that component. After the request completes, the instance is lost, and all its runtime values with it. Even if two requests are generated for a component in same session, nucleus will create two separate instances for the requested component.Request scope components are generally used for form handling. This is because, we dont want our form to be manipulated by multiple requests in same session or globally by any user across the world!!
NOTE: There might be situations when we want a redirect to take place in a request. So, we might want to preserve the request for the redirect too (i.e. making the redirect as a part of same request). This can be done using
HttpServletResponse.sendLocalRedirect() method. This method preserves the original request and the request-scoped components associated with it.
Other scopes viz. prototype and window also exist, but we will not cover them in detail right now.
NOTE: A very important point which needs to be noted here is that, a component can always refer to a component with a scope equal to a greater than its own scope. For example, a global scoped component CANNOT refer to a session or request scoped component. Below is the diagram which shows the acessibility of scopes:-
Let us now move forward and understand, how to extend a component...
Hi Monis,
ReplyDeleteWhen i try to create a ear file by using the command prompt command runassembler.bat -jboss -m i am getting an error the system cannot find the path specified. Please help me with the issue. I created the project in eclipse and build it suing eclipse. The project location is in ATG_HOME
drop me a mail at monis.yousuf@hotmail.com
ReplyDeletewith the full runAssembler command you are using along with the error you are getting.
Hi, Monis!
DeleteMay be you know answer on my interview question.
Why ForEach Droplet is global, but not request scope???
hi monis,
ReplyDeleteI have some specific question about this topic from interview.
ReplyDeleteMay be you can help me.
Why ForEach droplet is global scope but not request???
On the top of this page, Interview Questions -> General Topics
DeletePress CTRL + F and type "scope of a custom droplet and WHY?". This will answer your question.
How to access a lower scope component by an upper scope component.....?
ReplyDeleteUse the atg.servlet.ServletUtil api.
DeleteServletUtil has a static method called getCurrentRequest().
So you can chain the methods like this:-
ServletUtil.getCurrentRequest().resolveName("your-component-path") this will resolve a component of any scope.