Browse files

added the ability to reference js variables using the literal notation

such as myObject.myParam.myVar with jsvars['myObject.myParam.myVar'] = value
  • Loading branch information...
1 parent 41e1996 commit 7603cbcd27de72ad5519591d52ad3f8f6fcb213b @ejschmitt committed Sep 8, 2009
Showing with 12 additions and 4 deletions.
  1. +12 −4 lib/jsvars.rb
View
16 lib/jsvars.rb
@@ -17,9 +17,17 @@ def include_jsvars
close_tag_index = response.body.index /<\/body>/i
js_assignments = []
jsvars.each do |variable, value|
- js_assignments <<
-"if (typeof(#{ variable }) === 'object') {jsvars.objExtend(#{ variable }, #{ value.to_json });}
-else {var #{ variable } = #{ value.to_json }};"
+ js_assignments <<
+ if variable.to_s[/\./]
+ "#{ variable } = #{ value.to_json };"
+ else
+ "if (typeof(#{ variable }) === 'object') {
+ jsvars.objExtend(#{ variable }, #{ value.to_json });
+ }
+ else {
+ var #{ variable } = #{ value.to_json };
+ }"
+ end
end
methods =
@@ -29,7 +37,7 @@ def include_jsvars
for (var i = 1; i < arguments.length; i += 1) {
for (prop in arguments[i]){
if (arguments[i].hasOwnProperty(prop)) {
- mainObject[prop] = arguments[i][prop];
+ mainObject[prop] = (arguments[i][prop]);
}
}
}

0 comments on commit 7603cbc

Please sign in to comment.