Created
October 1, 2009 17:30
-
-
Save amiel/199115 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Base = {}; | |
// register a variable for use within the Base namespace | |
// there is an optional scope | |
// You may want to use the +js_var+ helper found in javascript_helper.rb | |
Base.register_variable = function(key, value, scope) { | |
if (scope) { | |
if (typeof Base[scope] === "undefined") Base[scope] = {}; | |
Base[scope][key] = value; | |
} else { | |
Base[key] = value; | |
} | |
}; | |
Base.reg = Base.register_variable; | |
// apply is used way to much here // | |
// log to the firebug console if window.console is available | |
Base.console = function(method) { | |
if (Base.DEBUG && window.console && window.console[method]) | |
window.console[method].apply(null, Array.prototype.slice.apply(arguments, [1])); | |
}; | |
// methods for logging | |
// this gives us Base.log, Base.console.log, Base.debug, etc | |
(function(methods){ | |
for (i in methods) | |
(function(method_name){ | |
Base[method_name] = Base.console[method_name] = function() { | |
Base.console.apply(null, [ method_name, 'Base', "[" + (new Date).toLocaleTimeString() + "]" ].concat(Array.prototype.slice.apply(arguments))); | |
}; | |
})(methods[i]); | |
})(['log', 'debug', 'info', 'warn', 'error']); | |
// all other firebug helpful methods | |
// this gives us Base.console.assert, Base.console.dir, etc | |
(function(methods){ | |
for (i in methods) | |
(function(method_name){ | |
Base.console[method_name] = function() { | |
Base.console.apply(null, [ method_name ].concat(Array.prototype.slice.apply(arguments))); | |
}; | |
})(methods[i]); | |
})(['assert', 'dir', 'dirxml', 'trace', 'group', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module JavascriptHelper | |
def js_vars_with_scope(scope) | |
@_js_var_scope = scope | |
yield | |
@_js_var_scope = nil | |
end | |
def js_var(key, value, scope = @_js_var_scope) | |
content_for(:in_javascript) { "Base.reg(#{ key.to_json }, #{ value.to_json }#{ ", " + scope.to_json if scope });" } | |
end | |
def assign_i18n_for_javasrcipt | |
js_var :I18n, I18n.backend.send(:translations)[I18n.locale.to_sym][:js] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment