Skip to content

Instantly share code, notes, and snippets.

@amiel
Created October 1, 2009 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/199115 to your computer and use it in GitHub Desktop.
Save amiel/199115 to your computer and use it in GitHub Desktop.
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']);
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