Skip to content

Commit ec99411

Browse files
committed
Merge pull request #10 from naveed-ahmad/master
Added ability to configure namespace
2 parents 39af334 + b50999e commit ec99411

File tree

9 files changed

+26
-12
lines changed

9 files changed

+26
-12
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ After the generator finishes, you will be prompted to add helper call to your ap
2424

2525
NOTE: Your JS files needed have been included before `include_rails_script`. In other words, you still need `<%= javascript_include_tag "application" %>` in your application layout.
2626

27+
## Configuration
28+
You can configure name of your application in config/initializers/rails_script.rb file.
29+
2730
## Usage
2831

2932
### Page (Action) Specific JavaScript
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
window.App ||= {}
2-
class App.<%= class_name.gsub('::', '') %>
1+
window.<%= RailsScript.app_namespace %> ||= {}
2+
class <%= RailsScript.app_namespace %>.<%= class_name.gsub('::', '') %>
33

44
constructor: ->
5-
return this
5+
return this

lib/generators/rails_script/controller/templates/javascript.js.coffee

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
window.App ||= {}
2-
class App.<%= controller.gsub('::', '') %> extends App.Base
1+
window.<%= RailsScript.app_namespace %> ||= {}
2+
class <%= RailsScript.app_namespace %>.<%= controller.gsub('::', '') %> extends <%= RailsScript.app_namespace %>.Base
33

44
beforeAction: (action) =>
55
return
@@ -22,4 +22,4 @@ class App.<%= controller.gsub('::', '') %> extends App.Base
2222

2323

2424
edit: =>
25-
return
25+
return

lib/generators/rails_script/install/install_generator.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class InstallGenerator < ::Rails::Generators::Base
66
def copy_files
77
template 'base.js.coffee', 'app/assets/javascripts/base.js.coffee'
88
template 'global.js.coffee', 'app/assets/javascripts/global.js.coffee'
9+
template 'rails_script.rb', 'config/initializers/rails_script.rb'
910
end
1011

1112
def create_directories

lib/generators/rails_script/install/templates/base.js.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
window.App ||= {}
2-
class App.Base
1+
window.<%= RailsScript.app_namespace %> ||= {}
2+
class <%= RailsScript.app_namespace %>.Base
33

44
constructor: ->
55
if (window.jQuery) then @setClearEventHandlers() # clearing application event handlers only possible with jQuery
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
window.App ||= {}
1+
window.<%= RailsScript.app_namespace %> ||= {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RailsScript.config do |c|
2+
# Configure name that will be used to namespace the javascript classes
3+
c.app_namespace = "App"
4+
end

lib/rails_script.rb

+6
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
require 'rails_script/railtie' if defined?(Rails)
55

66
module RailsScript
7+
mattr_accessor :app_namespace
8+
@@app_namespace = 'App'
9+
10+
def self.config
11+
yield self
12+
end
713
end

lib/rails_script/loader_helper.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def include_rails_script
77
Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};
88
99
(function() {
10-
window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
10+
window.$this = new (#{RailsScript.app_namespace}.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || #{RailsScript.app_namespace}.Base)();
1111
if (typeof $this.beforeAction === 'function') {
1212
$this.beforeAction("#{action_name}");
1313
}
@@ -18,8 +18,8 @@ def include_rails_script
1818
$this.afterAction("#{action_name}");
1919
}
2020
})();
21-
RUBY
21+
RUBY
2222
end
2323

2424
end
25-
end
25+
end

0 commit comments

Comments
 (0)