javascript - Initializing jQuery plugin with noConflict -


so i'm attempting work within constraints of request , have made progress bet still getting locked up. here's scenario:

currently, i'm working website depends on jquery v1.4.2. since access i'm given on backend ability inject content cms driven page, had been sideloading jquery 1.11 support needed functionality.

flash forward yesterday , made aware client's dev team has bundled v1.4 , v1.11 , has noconflicted 1.11 $$.

(function() {     $$ = $.noconflict( true );  // move jquery 1.11.0 $$ , restore $ jquery 1.4.2 })();  // use following self-invoking anonymous function  // when need run code depends on $ = jquery 1.11 // otherwise, can access jquery 1.11 $$ //  (function( $ ){ // inside here $ jquery 1.11 // , jquery 1.4.2 out of scope //  })( $$ ); 

since scripts (and plugin attempting load page) injected in middle of page , jquery v1.4 , v1.11 @ bottom of page, loading way:

(function checkforjquery(){     if ('$$' in window) {         (function($) {             console.log('success');             $.getscript("myplugin.jquery.js", function(){             $( ".devices").myplugin({                 // devices - array | productid                 prop1: ["prod3960155", "prod3640151", "prod3640152", "prod5530141"],                 prop2: "attribute",                 prop3: "attribute"             });         });     })($$);     } else {         console.log('not yet');         window.settimeout(checkforjquery, 1000);     } })(); 

so issue i'm having if use:

(function( $ ){})( $$ ); 

i nothing - plugin never initializes. however, if use:

(function( $ ){})( jquery ); 

it work, don't have access latest api. ideas why 1 work , not other?

for clarity, i'm using jquery plugin boilerplate , starts this:

;( function( $, window, document, undefined ) { } )( jquery, window, document ); 

any insights appreciated!

thanks, joe

the reason script won't work not call of plugin, creation.

to explain, code below uses second instance $$ search elements class .devices , starts plugin myplugin. nothing else.

(function($) {     $(".devices").myplugin(); })($$); 

but have registered plugin old version. reason why workes, when use jquery instead of $$ on code above.

to use $$ jquery instance in plugin, have set on creation too. there has passed in jquery. , because used noconflict, name jquery belongs old version of jquery.

to register plugin right instance, have change $$ on creation too.

;(function($, window, document, undefined){})($$, window, document); 

to make plugin working on other installations, can add jquery fallback. if use plugin on other sites , don't want change every time.

;(function($, window, document, undefined){})($$ || jquery, window, document); 

thats it.


Comments