Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AMD #111

Merged
merged 14 commits into from
May 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ pkg
.ruby-version

Gemfile.lock
gemfiles/*.lock
gemfiles/*.lock

.DS_Store
42 changes: 27 additions & 15 deletions lib/routes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 24 additions & 10 deletions lib/routes.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# root is this
root = (exports ? this)

ParameterMissing = (@message) -> #
ParameterMissing:: = new Error()
defaults =
Expand All @@ -13,8 +16,8 @@ Utils =
if !prefix and !(@get_object_type(object) is "object")
throw new Error("Url parameters should be a javascript hash")

if window.jQuery
result = window.jQuery.param(object)
if root.jQuery
result = root.jQuery.param(object)
return (if not result then "" else result)

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

namespace: (root, namespaceString) ->
# globalJsObject
createGlobalJsRoutesObject = ->
# namespace function, private
namespace = (mainRoot, namespaceString) ->
parts = (if namespaceString then namespaceString.split(".") else [])
return unless parts.length
current = parts.shift()
root[current] = root[current] or {}
Utils.namespace root[current], parts.join(".")

Utils.namespace window, "NAMESPACE"
window.NAMESPACE = ROUTES
window.NAMESPACE.options = defaults
mainRoot[current] = mainRoot[current] or {}
namespace mainRoot[current], parts.join(".")
# object
namespace(root, "NAMESPACE")
root.NAMESPACE = ROUTES
root.NAMESPACE.options = defaults
root.NAMESPACE
# Set up Routes appropriately for the environment.
if typeof define is "function" and define.amd
# AMD
define [], -> createGlobalJsRoutesObject()
else
# Browser globals
createGlobalJsRoutesObject()
42 changes: 42 additions & 0 deletions spec/js_routes/amd_compatibility_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "spec_helper"

describe JsRoutes, "compatibility with AMD/require.js" do

before(:each) do
evaljs("window.GlobalCheck = {};")
evaljs("window.define = function (requirs, callback) { window.GlobalCheck['js-routes'] = callback.call(this); return window.GlobalCheck['js-routes']; };")
evaljs("window.define.amd = { jQuery: true };")
strRequire =<<EOF
window.require = function (r, callback) {
var allArgs, i;

allArgs = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = r.length; _i < _len; _i++) {
i = r[_i];
_results.push(window.GlobalCheck[i]);
}
return _results;
})();

return callback.apply(null, allArgs);
};
EOF
evaljs(strRequire)
evaljs(JsRoutes.generate({}))
end

it "should working from global scope" do
expect(evaljs("Routes.inboxes_path()")).to eq(routes.inboxes_path())
end

it "should working from define function" do
expect(evaljs("Routes.inboxes_path()")).to eq(evaljs("GlobalCheck['js-routes'].inboxes_path()"))
end

it "should working from require" do
expect(evaljs("require(['js-routes'], function(r){ return r.inboxes_path(); })")).to eq(routes.inboxes_path())
end

end
4 changes: 2 additions & 2 deletions spec/js_routes/zzz_last_post_rails_init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
Rails.application.config.assets.initialize_on_precompile = true
end
it "should render some javascript" do
expect(ctx.evaluate('js-routes.js')).to match(/window\.Routes/)
expect(ctx.evaluate('js-routes.js')).to match(/root\.Routes/)
end
end
context "and not initialize on precompile" do
Expand All @@ -71,7 +71,7 @@
if 3 == Rails::VERSION::MAJOR
expect { ctx.evaluate('js-routes.js') }.to raise_error(/Cannot precompile/)
else
expect(ctx.evaluate('js-routes.js')).to match(/window\.Routes/)
expect(ctx.evaluate('js-routes.js')).to match(/root\.Routes/)
end
end
end
Expand Down