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.
$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 )
$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...