6

所以,我是 Require.js 的新手,我一直在通过使用 Require.js 方法加载各种其他库来学习这个库。

我已经成功加载了 Knockout.js 对象、Chart.js 对象以及自定义 Require.js 定义的对象。

但我似乎无法使用 Require.js 加载 jwplayer。这是我收到的错误方法: Uncaught TypeError: Cannot call method 'jwplayer' of undefined

这是我的示例代码(Knockout、Chart 对象均已成功加载)

require(['jwplayer/jwplayer', 'libs/Chart', 'libs/knockout-2.1.0', 'appViewModel','helper/util'], function(jwplayer, chart, ko, appViewModel, util) {

//LOADING FROM jwplayer.js
jwplayer("player").setup({
    width: '320',
    height: '40',
    sources: [{
        file: "rtmp://127.0.0.1:1935/vod/mp3:sample_1.mp3"
    },{
        file: "http://127.0.0.1:1935/vod/sample_1.mp3/playlist.m3u8"
    }]
});

//LOADING FROM Chart.js
var barChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            data : [65,59,90,81,56,55,40]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            data : [28,48,40,19,96,27,100]
        }
    ]   
};
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);

//LOADING FROM knockout-2.1.0.js
ko.applyBindings(new appViewModel());

//LOADING FROM A CUSTOM DEFINED OBJECT
util.greets(); 
    });

那么如何使用 Require.js 加载 jwplayer.js 呢?

4

1 回答 1

8

jwplayer.js 没有为 require.js 定义一个模块,所以你将不得不使用shim config,如下所示:

require.config({
    shim: {
        'jwplayer/jwplayer': {
            exports: 'jwplayer'
        }
    }
});

你可以在 requirejs api 文档中看到更多关于如何使用它的信息。

Edit: typo in code sample.

Edit 2: it should be noted that jwplayer() will return null if it can't find the player that you pass it, so even if it is loaded correctly, it will still throw that error. If you're getting the error even after including the configuration, try adding something like

console.log(jwplayer.api);

in the require callback and check your console to see if there's anything there.

于 2013-03-30T01:46:54.403 回答