billpull / knockout-bootstrap

A plugin that adds custom bindings for twitter bootstrap objects such as tooltips and popovers.
233 stars 69 forks source link

jQuery is not defined #55

Closed mikehaas763 closed 9 years ago

mikehaas763 commented 10 years ago

When using this lib with requirejs, and loading jquery with requirejs (rather than just another script being loaded through the DOM) I get this error when loading from a completely cleared cache:

Uncaught ReferenceError: jQuery is not defined

However, if I do a soft refresh I do not get that error. I haven't looked into why the issue is happening yet but am just documenting it here.

rockResolve commented 9 years ago

It is happening because the third function (creating $.fn.outerHtml) has a dependancy on global var jQuery.

Temporary workaround Add a shim to your require config e.g.

        require.config({
            ...
            paths: {
                ...
                "jQuery": "../Scripts/jquery-2.1.0",
                "koBootstrap": "../Scripts/knockout-bootstrap",
            shim: {
                ...
                "koBootstrap": {
                    deps: ["jQuery"],
                    exports: "$.fn.outerHtml"
                },
            }

Permanent Fix All code should be contained in the "setupKoBootstrap" factory.

Then jQuery can be added to the AMD define dependencies (at the bottom of the script). Note there are also many other references to $ in the factory already (as expected with knockout binding handlers) that should be satisfied with a jQuery dependency.

But why is outerHtml() created as a jQuery function? And why are S4() and guid() added as globals? Shouldn't they all be private functions?

benmosher commented 9 years ago

This can be closed now? I didn't think to put "closes #55" in my commits/PR. 😐

rockResolve commented 9 years ago

looks good to me!