1.1 --- a/docs/attributes.html Thu Jan 18 22:40:07 2007 +0000
1.2 +++ b/docs/attributes.html Thu Jan 18 22:40:48 2007 +0000
1.3 @@ -34,4 +34,24 @@
1.4 the attributes dictionary exists as long as the transaction object
1.5 itself. Should information need to be stored beyond the lifetime of a
1.6 transaction, the appropriate persistent information facilities should
1.7 -be used instead - see <a href="sessions.html">"Sessions and Persistent Information"</a> for more details.</p></body></html>
1.8 \ No newline at end of file
1.9 +be used instead - see <a href="sessions.html">"Sessions and Persistent Information"</a> for more details.</p><h2>Storing the Application Location in an Attribute</h2><p>One
1.10 +use of attributes is to take the location of an application, defined in
1.11 +terms of its path, and to store that information in an attribute. Such
1.12 +information would then travel to different resources in an application
1.13 +as part of the transaction, enabling those resources to generate things
1.14 +like hyperlinks which contain "absolute" rather than "relative"
1.15 +references to various parts of the application. Consider an application
1.16 +deployed at the following URL:</p><pre>http://application.business.com/bizapp/</pre><p>Inside this application, we may have other URLs which expose certain functions:</p><pre>http://application.business.com/bizapp/services/finance/accounting<br />http://application.business.com/bizapp/services/customer/invoice</pre><p>It
1.17 +may be interesting for these functions to know the location of the
1.18 +top-level or root of the application in order to, for example, generate
1.19 +hyperlinks to the top-level or to other established functions. Thus, we
1.20 +may decide to remember the top-level path which would be the following:</p><pre>/bizapp/</pre><p>Now, normally the <code>get_path_without_info</code> method will provide this information, but what if we wanted to remember the following path instead...?</p><pre>/bizapp/services/</pre><p>In
1.21 +fact, we can make use of the other path-related methods to obtain this
1.22 +path, provided that we ask for this information at the correct point in
1.23 +processing the request. Let us imagine that we have used the <a href="resource-map.html"><code>MapResource</code></a> class to build up the path hierarchy:</p><pre>resource = MapResource({<br /> "services" : MapResource({<br /> "finance" : finance_department,<br /> "customer" : customer_department<br /> })<br /> })</pre><p>We
1.24 +would want to define a resource to process the request and
1.25 +remember the path details just before choosing between the finance and
1.26 +customer departments. This resource would do the following:</p><ol><li>Ask for the path without info, along with the processed virtual path info (as described in <a href="path-info.html">"Paths To and Within Applications"</a>).</li><li>Join the paths together.</li><li>Store the result on an attribute (called "root", perhaps).</li><li>Invoke another resource.</li></ol><p>The advantage of doing this dynamically is that should we decide to change the location of the application or the path to <code>services</code> within it, we will not need to track down hard-coded path values and change them in lots of different places.</p><p>Fortunately, WebStack provides a class to do this work for us - <code>PathSelector</code> - and we can use it in the following way:</p><pre>from WebStack.Resources.Selectors import PathSelector<br /><br />resource = MapResource({<br /> "services" : PathSelector(<br /> MapResource({<br /> "finance" : finance_department,<br /> "customer" : customer_department<br /> })<br /> )<br /> })</pre><p>Now, once the <code>services</code> part of the path has been detected, the <code>PathSelector</code> resource will discover the path details, store them on an attribute, and then invoke the <code>MapResource</code> which chooses between departments, which in turn invokes other resources, and so on. However, all of the resources "below" the <code>PathSelector</code> resource
1.27 +will have an attribute called "root" which contains the selected "root
1.28 +path" of the application (or at least the interesting part of the
1.29 +application).</p><p>See the <a href="selectors.html">"Selectors"</a> document for more details of the <code>PathSelector</code> class.</p></body></html>
1.30 \ No newline at end of file