Alexander Nazarov (nfokz) wrote in changelog,
Alexander Nazarov
nfokz
changelog

[livejournal] r21986: LJSUP-12126: Function LJ::need_var() to ...

Committer: anazarov
LJSUP-12126: Function LJ::need_var() to insert variables into Site.page object
U   trunk/cgi-bin/ljlib.pl
U   trunk/cgi-bin/weblib.pl
Modified: trunk/cgi-bin/ljlib.pl
===================================================================
--- trunk/cgi-bin/ljlib.pl	2012-05-17 15:24:51 UTC (rev 21985)
+++ trunk/cgi-bin/ljlib.pl	2012-05-18 07:39:41 UTC (rev 21986)
@@ -2336,7 +2336,8 @@
     @LJ::NEEDED_RES = ();             # needed resources, in order requested (implicit dependencies)
                                       #  keys are relative from htdocs, values 1 or 2 (1=external, 2=inline)
     @LJ::INCLUDE_TEMPLATE = ();       # jQuery.tmpl templates (translated from HTML::Template)
-    %LJ::JSML = ();                   # Javascript language variables sent to the page header for further javascript use
+    %LJ::JSML  = ();                  # Javascript language variables sent to the page header for further javascript use
+    %LJ::JSVAR = ();                  # Javascript variables sent to the page header for further javascript use
     @LJ::INCLUDE_RAW = ();            # raw js/css added after needed resources.
     %LJ::REQ_GLOBAL = ();             # per-request globals
     %LJ::_ML_USED_STRINGS = ();       # strings looked up in this web request

Modified: trunk/cgi-bin/weblib.pl
===================================================================
--- trunk/cgi-bin/weblib.pl	2012-05-17 15:24:51 UTC (rev 21985)
+++ trunk/cgi-bin/weblib.pl	2012-05-18 07:39:41 UTC (rev 21986)
@@ -1541,12 +1541,15 @@
         my $site_params = LJ::js_dumper(\%site);
 
         my $journal_info_json = LJ::JSON->to_json(\%journal_info);
-        my $jsml_out = LJ::JSON->to_json(\%LJ::JSML);
-        my $site_version = LJ::ejs($LJ::CURRENT_VERSION);
+        my $jsml_out          = LJ::JSON->to_json(\%LJ::JSML);
+        my $jsvar_out         = LJ::JSON->to_json(\%LJ::JSVAR);
+        my $site_version      = LJ::ejs($LJ::CURRENT_VERSION);
+
         $ret_js .= qq {
             <script type="text/javascript">
                 Site = window.Site || {};
                 Site.ml_text = $jsml_out;
+                Site.page = $jsvar_out;
                 (function(){
                     var p = $site_params, i;
                     for (i in p) Site[i] = p[i];
@@ -2446,6 +2449,29 @@
     }
 }
 
+# Add some javascript variables
+sub need_var {
+    my %vars;
+
+    # Our arguments are hash ref
+    if (@_ == 1 and ref $_[0] eq 'HASH') {
+        %vars = %{$_[0]};
+    # List of key-value pairs otherwise
+    } else {
+        while (my ($k, $v) = splice @_, 0, 2) {
+            $vars{$k} = $v;
+        }
+    }
+
+    while (my ($k, $v) = each %vars) {
+        if ( exists $LJ::JSVAR{$k} ) {
+            warn 'JS Variable override: '. $k;
+        }
+
+        $LJ::JSVAR{$k} = $v;
+    }
+}
+
 sub set_remote_language {
     my ($lang) = @_;
 

Tags: anazarov, livejournal, nfokz, pl
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments