[Rt-commit] rt branch, 4.2/config-in-js, created. rt-4.0.7-323-g337a2fa

Thomas Sibley trs at bestpractical.com
Mon Sep 24 22:03:45 EDT 2012


The branch, 4.2/config-in-js has been created
        at  337a2fafe8210d4a3794bcfe2732ce7558d0e76b (commit)

- Log -----------------------------------------------------------------
commit 337a2fafe8210d4a3794bcfe2732ce7558d0e76b
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Mon Sep 24 19:03:28 2012 -0700

    Embed config and current user data into each page for javascript
    
    Enables smarter, more dynamic, more cacheable JS which doesn't need to
    rely on being generated by Mason.
    
    There is much more data likely to be useful in both of these data
    structures; this is just a start.

diff --git a/share/html/Elements/HeaderJavascript b/share/html/Elements/HeaderJavascript
index d5741f4..7c9c4b8 100644
--- a/share/html/Elements/HeaderJavascript
+++ b/share/html/Elements/HeaderJavascript
@@ -49,6 +49,7 @@
 $focus => undef
 $onload => undef
 </%args>
+<& JavascriptConfig &>
 
 % for my $jsfile ( @js_files ) {
 <script type="text/javascript" src="<%RT->Config->Get('WebPath')%>/NoAuth/js/<% $jsfile %>"></script>
diff --git a/share/html/Elements/JavascriptConfig b/share/html/Elements/JavascriptConfig
new file mode 100644
index 0000000..f986136
--- /dev/null
+++ b/share/html/Elements/JavascriptConfig
@@ -0,0 +1,33 @@
+<%init>
+my @CurrentUser = qw(id Name EmailAddress);
+my @Config      = qw(rtname WebPath);
+
+$m->callback(
+    CallbackName    => "Keys",
+    CurrentUser     => \@CurrentUser,
+    Config          => \@Config,
+);
+</%init>
+<script>
+window.RT = {};
+
+%# We don't use JSON() here because it doesn't sufficiently escape for
+%# embedding in HTML.
+% if ($session{CurrentUser} and $session{CurrentUser}->id) {
+RT.CurrentUser = {
+% for my $attr (@CurrentUser) {
+    <% $attr |n,j %>: <% $session{CurrentUser}->$attr |n,j %>,
+% }
+    "Privileged": <% $session{CurrentUser}->Privileged ? 'true' : 'false' %>
+};
+% } else {
+RT.CurrentUser = {};
+% }
+
+RT.Config = {
+% for my $option (@Config) {
+    <% $option |n,j %>: <% RT->Config->Get($option, $session{CurrentUser}) |n,j %>,
+% }
+    "__end__": "avoids dealing with chomping the trailing comma"
+};
+</script>

-----------------------------------------------------------------------


More information about the Rt-commit mailing list