2013-05-11 26 views
1

我試圖把一個簡單的jqPlot在我的Rails應用程序,但我不能得到它加載..就是在我的觀點:加載簡單jqPlot使用jQuery Mobile和Rails的

<div id='chart1' style="height:400px;width:300px; "></div> 

<script> 
$(document).bind('pageinit', function() { 
     var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]); 
}); 
</script> 

當我打開網頁時,jQuery Mobile的微調那張永遠和我得到這個錯誤在JavaScript控制檯:

Uncaught TypeError: Object function (e,t){return new b.fn.init(e,t,r)} has no method 'jqplot'

我在我的應用程序中加入適當的jqPlot javascript文件到我的供應商/資產的文件夾,並引用他們.js,所以我不認爲這是問題所在。

有趣的是(或許不是),如果我在的JavaScript塊的開始添加一行

jQuery.noConflict(); 

,然後我沒有得到任何錯誤和頁面加載罰款,但沒有jqPlot。然而,然後我可以從JavaScript控制檯引用jqPlot。

我對網絡開發很新,所以我可能缺少一些基本的東西..任何幫助將不勝感激!

+0

顯示爲您的頁面示例。此錯誤指出jqPlot函數不存在,這意味着它是jqPlot js文件可能未初始化(或inncorectly初始化)。我可以告訴你,你不需要jQuery.noConflict(); jqPlot在jQuery Mobile中工作得很好。 – Gajotres 2013-05-11 18:00:13

+0

那麼我的供應商/ assets/javascripts文件夾中有jquery.jqplot.min.js文件,並且在我的application.js中用'// = require jquery.jqplot.min.js'引用它。我也是這樣做的與jqPlot .css文件的東西。還有另外一種方法我應該初始化它,還是我誤解了你? – jmellman 2013-05-11 18:04:07

+0

哦,你正在使用require,這可能是一個問題。因爲它使用的是異步js加載,所以也許jqplot在pageinit事件之後被初始化。嘗試使用pageshow事件。 – Gajotres 2013-05-11 18:11:16

回答

1

我有一個plot模塊(文件jqplot.module.js),它看起來像這樣:

define([ 
     '../js/plugins/jqplot/jquery.jqplot' 
    , 'css!../js/plugins/jqplot/jquery.jqplot' 
], 
function() { 
    var plot; 
    require([ 
     '../js/plugins/jqplot/plugins/jqplot.barRenderer' 
     , '../js/plugins/jqplot/plugins/jqplot.logAxisRenderer' 
     , '../js/plugins/jqplot/plugins/jqplot.categoryAxisRenderer' 
     , '../js/plugins/jqplot/plugins/jqplot.canvasAxisTickRenderer' 
     , '../js/plugins/jqplot/plugins/jqplot.canvasTextRenderer' 
     , '../js/plugins/jqplot/plugins/jqplot.pointLabels' 
     , '../js/plugins/jqplot/plugins/jqplot.enhancedLegendRenderer' 
     ], 
    function() { 
     plot = $.jqplot; 
    }); 
    return plot; 
    } 
); 

我把它稱爲在頁面上的問題是這樣的:

require(['plot'], 
    function() { 
    // in here I have $.jqplot available 
    } 
); 

裏面我main.js我宣佈路徑爲jqplot.module

plot:'../js/plugins/jqplot/jqplot.module' 

適合我工作。