2012-09-13 20 views
6

Vì vậy, tôi đang làm việc với jquery.flot và jquery.flot.selection và kể từ khi xác định ({... tải mô-đun không đồng bộ Tôi gặp sự cố vì lựa chọn plugin đang cố đẩy chính nó vào $ .plot.plugins (được tạo bởi jquery.flot) nhưng tại thời điểm đó $ .plot.plugins vẫn không được xác định.yêu cầu thiết lập shim- jquery.flot/jquery.flot.selection

Tôi thấy rằng đối số "shim" trong yêu cầu .config nên giúp tôi với điều này nhưng tôi không có may mắn ... vì vậy đây là tóm tắt ... jquery.flot tạo $ .plot và jquery.flot.selection thêm nó vào $ .plot.plugins

những gì tôi đã thử ...

shim:{ 
    'js/lib/jquery.flot':{ 
     exports:'$.plot' 
    }, 
    'js/lib/jquery.flot.selection':{ 
     deps:['js/lib/jquery.flot'] 
    } 
} 

cũng

shim:{ 
    'js/lib/jquery.flot.selection':['js/lib/jquery.flot'] 
} 

Plugin của tôi trông như thế này ..

define(['jquery','lib/jquery.flot','lib/jquery.flot.selection'], function() { 
(function($) { 
    // jQuery plugin definition 
..... 

Tôi cũng đã cố gắng

define(['jquery'],function(){ 
require[('js/lib/jquery.flot.selection'],function(){ 
//jQuery plugin definition 
... 

Tôi nên làm gì ???

+1

Hóa ra tôi đang sử dụng RequireJs 1.0.4, vì vậy tôi không thể dựa vào shim ... Tôi có thể làm gì? – Joe

+0

Shim được lấy cảm hứng từ Use.js, một plugin dành cho 1.x - http://tbranyen.com/post/amdrequirejs-shim-plugin-for-loading-incompatible-javascript –

+0

Tuyệt vời! vì vậy tôi đã thêm phần này vào plugin của tôi "define ([" jquery "," lib/jquery.flot "," lib/use! lib/jquery.flot.selection "]," và địa chỉ này của tôi yêu cầu " đường dẫn: { ... sử dụng: '../../../../otherDirec/src/main/webapp/js/lib/use' }, sử dụng: { "../../ ../../ otherDirec/src/main/webapp/js/lib/jquery.flot.selection ": { deps: [" sử dụng! ../../../../ otherDirec/src/main /webapp/js/lib/jquery.flot "] } }" và tôi vẫn gặp lỗi khi nói Mô-đun 'lib/jquery.flot.selection' chưa được xác định hoặc không có cấu hình 'use'. Hãy chắc chắn rằng tồn tại, thêm cấu hình 'use' hoặc không sử dụng! trên đó – Joe

Trả lời

5

Đối với bất kỳ ai tình cờ gặp phải vấn đề này trong tương lai, RequireJS có thể hơi mờ khi nói đến chẩn đoán sự cố. Here's một ví dụ làm việc của RequireJS 2 với jquery.flot và jquery.flot.selection, không có mô-đun sử dụng.

Hơi khó để biết điều gì khác biệt giữa ví dụ này và câu hỏi ở trên, do đó sự không rõ ràng!

require.config({ 
    shim: { 
     'jquery': { 
      exports: '$' 
     }, 
      'jquery.flot': { 
      deps: ['jquery'], 
      exports: '$.plot' 
     }, 
      'jquery.flot.selection': { 
      deps: ['jquery.flot'] 
     } 
    }, 
    paths: { 
     'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min', 
      'jquery.flot': '//cdnjs.cloudflare.com/ajax/libs/flot/0.8.1/jquery.flot.min', 
      'jquery.flot.selection': 'https://raw.github.com/flot/flot/master/jquery.flot.selection' 
    } 
}); 

require(['jquery', 'jquery.flot', 'jquery.flot.selection'], function ($) { 
    // jQuery plugin definition 
    console.log($.plot); 
    $('#x').text($.plot.plugins); 
});