2013-07-31 125 views
1

我正在嘗試使用jqplot與jquery mobile,牽線木偶和requirejs。我已經囊括了所有jqplot需要CSS,以及在head標籤的腳本文件,但是當我嘗試使用下面的代碼Jqplot不與requirejs,jquerymobile,木偶

define([ 'plot' ], 
    function() { 
console.log("Success..Inside Offer Page Script."); 
console.log("Plot..."+$.jqplot); 
$.jqplot.config.enablePlugins = true; 
var s1 = [ 2, 6, 7, 10 ]; 
var ticks = [ 'a', 'b', 'c', 'd' ]; 

plot1 = $.jqplot('chart1', [ s1 ], { 
    seriesDefaults : { 
     renderer : $.jqplot.BarRenderer, 
     pointLabels : { 
      show : true 
     } 
    }, 
    axes : { 
     xaxis : { 
      renderer : $.jqplot.CategoryAxisRenderer, 
      ticks : ticks 
     } 
    }, 
    highlighter : { 
     show : false 
    } 
}); 
}); 

繪製圖表它給了我這樣的錯誤

Uncaught TypeError: undefined is not a function jqplot.barRenderer.js:41 
(line 41: $.jqplot.BarRenderer.prototype = new $.jqplot.LineRenderer();) 

Uncaught TypeError: Cannot call method 'push' of undefined jqplot.pointLabels.js:377 
(line 377: $.jqplot.postSeriesInitHooks.push($.jqplot.PointLabels.init);) 

中的情節我的上述代碼的定義是

define([ 
    '../scripts/ext_libs/jquery.jqplot', 'jquery' 
], 
function() { 
var plot; 
require([ 
    '../scripts/ext_libs/jqplot.barRenderer', 
    '../scripts/ext_libs/jqplot.pointLabels', 
    '../scripts/ext_libs/jqplot.categoryAxisRenderer', 
    ], 
function() { 
    plot = $.jqplot; 
}); 
return plot; 

} );

任何人都可以請幫助我如何解決這些錯誤?

在此先感謝。

+0

不要你必須把'在.js'擴展陳述事實文件結尾。 我認爲它無法找到barRenderer的代碼。 – Gyandeep

+0

@Gyandeep感謝您的回覆,但requirejs已經假定給定的路徑只包含js文件。所以它不是必需的。 – Rachna

+0

因爲當我運行你的代碼它運行良好,我使用純JavaScript。我認爲你的包含文件存在一些問題。 – Gyandeep

回答

1

嘗試使用全局配置對象,而不是,看看這個例子:https://github.com/davidsulc/structuring-backbone-with-requirejs-and-marionette/blob/master/assets/js/require_main.js

這是從我的新書木偶& RequireJS(https://leanpub.com/structuring-backbone-with-requirejs-and-marionette),並宣佈一些jQuery插件(如jQuery UI的)。

你有沒有試圖讓你的情節有jQuery作爲依賴?看起來就是這個問題。

你可能需要一個配置看起來像這樣:

requirejs.config({ 
    paths: { 
    jquery: "path/to/jquery", 
    jqplot: "path/to/jqplot", 
    "jqplot.barRenderer": "path/to/jqplot.barRenderer", 
    "jqplot.pointLabels": "path/to/jqplot.pointLabels", 
    "jqplot.categoryAxisRenderer": "path/to/jqplot.categoryAxisRenderer" 
    }, 
    shim: { 
    jqplot: ["jquery"], 
    "jqplot.barRenderer": ["jqplot"], 
    "jqplot.pointLabels": ["jqplot"], 
    "jqplot.categoryAxisRenderer": ["jqplot"] 
    } 
}); 

這表明「jqplot」依賴於jQuery和插件依賴於「jqplot」。然後,你可以在你的代碼有這樣的定義情節:

define(['jqplot.barRenderer', 'jqplot.pointLabels', 'jqplot.categoryAxisRenderer'],function() { 
    return $.jqplot; 
}); 

當插件已經被加載這將返回jqplot財產。然後,您的代碼可以是:

define([ 'plot' ], function() { 
    console.log("Success..Inside Offer Page Script."); 
    console.log("Plot..."+$.jqplot); 
}); 
+0

感謝@David Sulc的回覆,但我試過這樣做。我也添加jquery依賴,但仍然是相同的錯誤。 Cna你請別人指點別的嗎?還有一個問題,在jquery的'document.ready'中寫劇情創建代碼是否是強制性的?腳本是否應該在頁面完全創建後運行? – Rachna

+0

我編輯了我的答案,以引用您可能想要嘗試的示例全局配置。我不相信您的問題與'document.ready'有關,但繪圖代碼應該很可能在視圖的onShow中'確保DOM元素存在的方法。 –

+0

我這樣做只有@David Sulc,對於requirejs我有一個配置文件,其中我給這個plot的路徑爲'plot:'../ scripts/utils/jqplot.module',但仍然有錯誤:( – Rachna

0

討厭的問題,因爲它是

jQuery是需要jqplot三個依賴性鏈這是需要jqplot插件,我基於相同的線條爲簡單的解決方案上述

第一個做你的requirejs「main.js」的配置,像這樣

requirejs.config({ 
    paths: { 
     "jquery": "path/to/jquery-1.10.2.min", 

     // WORKAROUND : jQuery plugins + shims 
     "jqplot.core": "path/to/jquery-jqplot-1.0.8.min", 
     "jqplot": "jquery-jqplot-module-with-plugins-1.0.8" 
    }, 
    shim: { 
     "jqplot.core": {deps: ["jquery"]}, 
     "jqplot": {deps: ["jqplot.core"]} 
    } 
}); 

創建一個名爲「jQuery的jqplot模塊與 - PLU一個封裝文件模塊文件軋花-1.0.8.js」,包含:

// wraps jquery jqplot plugin in define statement 
define([ 
    "jquery", 
    "path/to/jqplot.highlighter.min", 
    "path/to/jqplot.cursor.min", 
    "path/to/jqplot.dateAxisRenderer.min", 
    "path/to/jqplot.canvasTextRenderer.min", 
    "path/to/jqplot.canvasAxisLabelRenderer.min", 
    "path/to/jqplot.enhancedLegendRenderer.min", 
    "path/to/jqplot.pieRenderer.min", 
    "path/to/jqplot.donutRenderer.min", 
], function($) { 
    var jqplot; 
    jqplot = $.jqplot; 
    return jqplot; 
}); 

然後當你永遠需要使用這些插件jqplot,乾脆喊出了‘jqplot’,這將載入‘jQuery的’,然後‘jqplot.core’,那麼所有的jqplot模塊,最後返回jqplot對象:)

require(["jquery", "jqplot"], function ($, $jqplot) { 
    console.log("Success..Inside Require JS"); 
    console.log("Plot...", $.jqplot, $jqplot); 
}); 

define(["jquery", "jqplot"], function ($, $jqplot) { 
    console.log("Success..Inside Define JS"); 
    console.log("Plot...", $.jqplot, $jqplot); 
}); 

田田!:)

PS jQuery插件是邪惡的,沒有建議如何解決這種情況,只是一個

歡呼

螞蟻