-
-
Save tbranyen/9255362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use an AMD package here to gain access to nested internal modules. | |
require.config({ | |
packages: [{ | |
name: "jquery", | |
location: "vendor/jquery/src", | |
main: "index.js" | |
}] | |
}); | |
// If we are using AMD, we don't care about core. | |
define(function(require, exports, module) { | |
"use strict" | |
var ajax = require("jquery/ajax"); | |
var data = require("jquery/data"); | |
var req = ajax.get("/modular"); | |
req.then(function() { | |
// Modules, sucka. | |
}); | |
exports.ajax = ajax; | |
exports.data = data; | |
}); | |
// jquery/html would return a module representing the function. | |
require(["jquery/html"], function(html) { | |
html.call(elems, "value"); | |
}); | |
// Want chaining instead, hold onto your seats!!!! | |
require(["jquery/core", "jquery/html"], function($, html) { | |
// Attach to core. | |
$.fn.html = html; | |
// Now that's what I'm talking about. | |
$(".some-elems").html("value"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I disagree, only in that I think it's possible for jQuery to have both. However, As I stated before:
It may be a little too aggressive for jQuery to migrate away from the current syntax, and I'm not 100% sure if it's in its best interest; chaining is a valuable thing. Regardless, that doesn't mean we can't have chaining and AMD.
My original example was exactly that:
https://gist.github.com/davearel/9254418
Of course, that doesn't describe the actual implementation, but rather the outcome. The point is simply that we extend the core jQuery namespace, with the necessary dependencies, to a LOCAL variable. That way it is only accessible to that particular module.
The require method would return a new instance of jQuery that only contains the core methods and the dependency methods.