Skip to content

Commit 8fad914

Browse files
committed
Merge pull request #111 from le0pard/master
Support AMD
2 parents b314b5d + f72f96b commit 8fad914

File tree

5 files changed

+98
-28
lines changed

5 files changed

+98
-28
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ pkg
5151
.ruby-version
5252

5353
Gemfile.lock
54-
gemfiles/*.lock
54+
gemfiles/*.lock
55+
56+
.DS_Store

lib/routes.js

Lines changed: 27 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/routes.js.coffee

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# root is this
2+
root = (exports ? this)
3+
14
ParameterMissing = (@message) -> #
25
ParameterMissing:: = new Error()
36
defaults =
@@ -13,8 +16,8 @@ Utils =
1316
if !prefix and !(@get_object_type(object) is "object")
1417
throw new Error("Url parameters should be a javascript hash")
1518

16-
if window.jQuery
17-
result = window.jQuery.param(object)
19+
if root.jQuery
20+
result = root.jQuery.param(object)
1821
return (if not result then "" else result)
1922

2023
s = []
@@ -221,17 +224,28 @@ Utils =
221224
@_classToTypeCache["[object #{name}]"] = name.toLowerCase()
222225
@_classToTypeCache
223226
get_object_type: (obj) ->
224-
return window.jQuery.type(obj) if window.jQuery and window.jQuery.type?
227+
return root.jQuery.type(obj) if root.jQuery and root.jQuery.type?
225228
return "#{obj}" unless obj?
226229
(if typeof obj is "object" or typeof obj is "function" then @_classToType()[Object::toString.call(obj)] or "object" else typeof obj)
227230

228-
namespace: (root, namespaceString) ->
231+
# globalJsObject
232+
createGlobalJsRoutesObject = ->
233+
# namespace function, private
234+
namespace = (mainRoot, namespaceString) ->
229235
parts = (if namespaceString then namespaceString.split(".") else [])
230236
return unless parts.length
231237
current = parts.shift()
232-
root[current] = root[current] or {}
233-
Utils.namespace root[current], parts.join(".")
234-
235-
Utils.namespace window, "NAMESPACE"
236-
window.NAMESPACE = ROUTES
237-
window.NAMESPACE.options = defaults
238+
mainRoot[current] = mainRoot[current] or {}
239+
namespace mainRoot[current], parts.join(".")
240+
# object
241+
namespace(root, "NAMESPACE")
242+
root.NAMESPACE = ROUTES
243+
root.NAMESPACE.options = defaults
244+
root.NAMESPACE
245+
# Set up Routes appropriately for the environment.
246+
if typeof define is "function" and define.amd
247+
# AMD
248+
define [], -> createGlobalJsRoutesObject()
249+
else
250+
# Browser globals
251+
createGlobalJsRoutesObject()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require "spec_helper"
2+
3+
describe JsRoutes, "compatibility with AMD/require.js" do
4+
5+
before(:each) do
6+
evaljs("window.GlobalCheck = {};")
7+
evaljs("window.define = function (requirs, callback) { window.GlobalCheck['js-routes'] = callback.call(this); return window.GlobalCheck['js-routes']; };")
8+
evaljs("window.define.amd = { jQuery: true };")
9+
strRequire =<<EOF
10+
window.require = function (r, callback) {
11+
var allArgs, i;
12+
13+
allArgs = (function() {
14+
var _i, _len, _results;
15+
_results = [];
16+
for (_i = 0, _len = r.length; _i < _len; _i++) {
17+
i = r[_i];
18+
_results.push(window.GlobalCheck[i]);
19+
}
20+
return _results;
21+
})();
22+
23+
return callback.apply(null, allArgs);
24+
};
25+
EOF
26+
evaljs(strRequire)
27+
evaljs(JsRoutes.generate({}))
28+
end
29+
30+
it "should working from global scope" do
31+
expect(evaljs("Routes.inboxes_path()")).to eq(routes.inboxes_path())
32+
end
33+
34+
it "should working from define function" do
35+
expect(evaljs("Routes.inboxes_path()")).to eq(evaljs("GlobalCheck['js-routes'].inboxes_path()"))
36+
end
37+
38+
it "should working from require" do
39+
expect(evaljs("require(['js-routes'], function(r){ return r.inboxes_path(); })")).to eq(routes.inboxes_path())
40+
end
41+
42+
end

spec/js_routes/zzz_last_post_rails_init_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
Rails.application.config.assets.initialize_on_precompile = true
6161
end
6262
it "should render some javascript" do
63-
expect(ctx.evaluate('js-routes.js')).to match(/window\.Routes/)
63+
expect(ctx.evaluate('js-routes.js')).to match(/root\.Routes/)
6464
end
6565
end
6666
context "and not initialize on precompile" do
@@ -71,7 +71,7 @@
7171
if 3 == Rails::VERSION::MAJOR
7272
expect { ctx.evaluate('js-routes.js') }.to raise_error(/Cannot precompile/)
7373
else
74-
expect(ctx.evaluate('js-routes.js')).to match(/window\.Routes/)
74+
expect(ctx.evaluate('js-routes.js')).to match(/root\.Routes/)
7575
end
7676
end
7777
end

0 commit comments

Comments
 (0)