Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added a better method to check for variable existance before trying t…

…o assign it
  • Loading branch information...
commit 9c4163b1301d71b0ecfe0df640184bfb6d9d7d5d 1 parent 7603cbc
@ejschmitt authored
Showing with 12 additions and 1 deletion.
  1. +12 −1 lib/jsvars.rb
View
13 lib/jsvars.rb
@@ -19,7 +19,18 @@ def include_jsvars
jsvars.each do |variable, value|
js_assignments <<
if variable.to_s[/\./]
- "#{ variable } = #{ value.to_json };"
+ # allows usage like jsvars['myObj.myVar.myValue] = "number"
+ object_tests = ""
+ possible_objects = variable.split('.')
+ possible_objects.each_with_index do |obj, i|
+ full_obj = possible_objects[0..i].join('.')
+ object_tests <<
+ "if(#{ full_obj } === undefined) {
+ #{ "var" if i == 0 } #{ full_obj } = {};
+ }
+ "
+ end
+ object_tests + "#{ variable } = #{ value.to_json };"
else
"if (typeof(#{ variable }) === 'object') {
jsvars.objExtend(#{ variable }, #{ value.to_json });

0 comments on commit 9c4163b

Please sign in to comment.
Something went wrong with that request. Please try again.