2012-12-18 14 views
7

I plug-in richiedono quasi sempre il caricamento prima di jQuery. E non dovrebbero farlo a causa del mio uso dello shim setting.RequireJS non funziona con i plugin jQuery Shim'ed

Nei miei main.js ho queste impostazioni:

requirejs.config({ 
    paths: { 
     'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min', 
     'bootstrap': '../bootstrap/js/bootstrap.min', 
     'select2': 'vendor/select2', 
     'jshashtable': 'vendor/jshashtable-2.1', 
     'jquery.numberformatter': 'vendor/jquery.numberformatter-1.2.3.min', 
     'jq-datepicker': 'vendor/bootstrap-datepicker', 
     'jq-datepicker.da': 'vendor/bootstrap-datepicker.da' 
    }, 

    // Use shim for plugins that does not support ADM 
    shim: { 
     'bootstrap': ['jquery'], 
     'select2': ['jquery'], 
     'jq-datepicker': ['jquery'], 
     'jshashtable': ['jquery'], 
     'jquery.numberformatter': ['jquery', 'jshashtable'] 
    }, 
    enforceDefine: true 
}); 

avanti in questo file ho il seguente:

// Start the main app logic. 
requirejs(['jquery', 'bootstrap', 'jq-datepicker'], 
function ($) { 

    console.log('Require.JS loaded'); 

    // Init datepickers 
    // Docs: https://github.com/eternicode/bootstrap-datepicker 
    $('.datepicker').datepicker({ 
     format: 'dd/mm/yyyy', 
     language: 'da', 
     keyboardNavigation: false, 
     autoclose: true 
    }); 

}); 

Ma io ottenere questo errore per tutto il tempo:

Uncaught TypeError: undefined is not a function bootstrap.min.js:6 
(anonymous function) bootstrap.min.js:6 
(anonymous function) 

E posso vedere nella mia scheda Rete di Chrome che viene caricato prima di jQuery.

Ora ho provato ad aggiungere il enforceDefine: true dopo aver guardato qui intorno su StackOverflow, ma senza fortuna. Ho provato a spostare il requirejs.config nella mia pagina html. E ho provato a caricare jQuery da un file locale. Tutto senza fortuna.

Cosa mi manca?

+0

ho testato il tuo esempio e non vedere eventuali problemi: http://jsfiddle.net/asgoth/tHkHw/Ho rimosso enforceDefine e jq-datepicker.da (impossibile trovarlo) – asgoth

risposta

5

Hai Shim jquery troppo?

shim: { 
    jQuery: { 
     exports: 'jquery' 
    }, 
    'bootstrap': { 
     exports : 'jquery' 
    }, 
    'select2': { 
     exports :'jquery' 
    }, 
    ... 
}, 
+0

Sì, ho provato anche questo ma senza fortuna a ora sto caricando jQuery al di fuori di RequireJS. –

+1

bootstrap: { deps: ['jQuery'], esportazioni: "jQuery" }, –

+0

Ho risolto questo problema, ma è passato qualche tempo. Si ottiene l'accettazione come nella maggior parte dei casi questa sarebbe la verità. E tu hai il più upvotes. –

1

Prova:

shim: { 
    'bootstrap': { 
     exports : 'jquery' 
    }, 
    'select2': { 
     exports :'jquery' 
    }, 
    ... 
}, 
+0

Ho già provato questo, non ha funzionato :( –

+0

Questo non ha funzionato neanche per me :( –

2

Non è lo $ che si desidera esportare?

shim: { 
     'jquery': { 
      exports: '$' 
     }, 
     'jqueryuitouchpunch': { 
      deps: ['jqueryui'] 
     }, 
} 
0

Solo garantire che si carica jQuery prima del vostro plugin, utilizzando definire:

// Start the main app logic. 
requirejs(['jquery', 'bootstrap'], function ($) { 
    define(['jq-datepicker'], function() { 
     $('.datepicker').datepicker({ 
      format: 'dd/mm/yyyy', 
      language: 'da', 
      keyboardNavigation: false, 
      autoclose: true 
    }); 
    }); 

}); 
Problemi correlati