Permalink
Browse files

Implement a more general-purpose approach to invoking jQuery selector…

… functions
  • Loading branch information...
1 parent 75a4caa commit d420dea47af0763510128d281358dc3b8131e6a8 @hlship committed Nov 11, 2011
Showing with 38 additions and 14 deletions.
  1. +0 −9 resources/cascade/bootstrap-init.js
  2. +23 −4 resources/cascade/init.js
  3. +14 −0 src/cascade/import.clj
  4. +1 −1 src/main.clj
View
9 resources/cascade/bootstrap-init.js
@@ -1,9 +0,0 @@
-define(["jquery", "./bootstrap-twipsy", "./bootstrap-popover"],
- function($) {
-
- return {
- popover: function(selector, options) {
- $(selector).popover(options);
- }
- };
- });
View
27 resources/cascade/init.js
@@ -1,8 +1,8 @@
define(['jquery'], function($) {
- function doInit(moduleName, functionName, args) {
+ function doInit(moduleName, functionName, initArgs) {
require([moduleName], function(module) {
- module[functionName].apply(null, args);
+ module[functionName].apply(null, [initArgs]);
});
}
@@ -22,8 +22,27 @@ define(['jquery'], function($) {
});
}
- // In the future, there'll be additional function(s) used when handling a partial render Ajax response.
+ function doInvoke(dependencies, selector, functionName, args) {
+
+ var targetArguments = $.makeArray(arguments).slice(3);
+
+ require(dependencies, function() {
+ var selection = $(selector);
+ selection[functionName].apply(selection, targetArguments);
+ });
+ }
+
+ /**
+ * Invoke an arbitrary jQuery method on a selector.
+ * @param args dependencies, selector, functionName, args to pass to the function
+ */
+ function invoke(invokeArgs) {
+ doInvoke.apply(null, invokeArgs);
+ }
+
+
return {
- pageInit : init
+ pageInit : init,
+ invoke : invoke
};
});
View
14 src/cascade/import.clj
@@ -73,6 +73,20 @@ arguments
; TODO: this may change a bit when we implement Ajax partial rendering.
(import-into-keyed-list :javascript [module-name initializer-fn-name arguments]))
+(defn javascript-invoke
+ "Builds a very particular kinds of call to the javascript function:
+dependencies
+ List of RequireJS module dependencies necessary to invoke the function. These are typically dependencies that extend
+ jQuery.
+selector
+ A CSS selector string passed to the jQuery main function.
+function-name
+ Name of a jQuery function to invoke on the selection.
+arguments
+ Arguments to pass to the jQuery function."
+ [dependencies selector function-name & arguments]
+ (apply javascript "cascade/init" "invoke" dependencies selector function-name arguments))
+
(defn to-element-node
"Converts an asset into a <link> element node."
[asset]
View
2 src/main.clj
@@ -25,7 +25,7 @@
]])
(defview hello-world [req]
- (javascript "cascade/bootstrap-init" "popover" "#force-failure" {})
+ (javascript-invoke ["cascade/bootstrap-twipsy" "cascade/bootstrap-popover"] "#force-failure" "popover" {})
(layout "Cascade Hello World"
(markup
:div.alert-message.success>p [

0 comments on commit d420dea

Please sign in to comment.