Add an error message if the graph placeholder isn't found.

This commit is contained in:
Thomas Pelletier 2012-05-11 18:25:29 +01:00
parent c66a723acc
commit 2108baaf4e
4 changed files with 21 additions and 2 deletions

View File

@ -14,6 +14,10 @@ class Morris.Line
@el = $ document.getElementById(options.element) @el = $ document.getElementById(options.element)
else else
@el = $ options.element @el = $ options.element
if @el == null || @el.length == 0
throw new Error("Graph placeholder not found.")
@options = $.extend {}, @defaults, options @options = $.extend {}, @defaults, options
# backwards compatibility for units -> postUnits # backwards compatibility for units -> postUnits
if typeof @options.units is 'string' if typeof @options.units is 'string'

View File

@ -29,6 +29,9 @@
} else { } else {
this.el = $(options.element); this.el = $(options.element);
} }
if (this.el === null || this.el.length === 0) {
throw new Error("Graph placeholder not found.");
}
this.options = $.extend({}, this.defaults, options); this.options = $.extend({}, this.defaults, options);
if (typeof this.options.units === 'string') { if (typeof this.options.units === 'string') {
this.options.postUnits = options.units; this.options.postUnits = options.units;

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -11,7 +11,7 @@
module("Morris"); module("Morris");
test("Input data remains untouched", function() { test("Input data remains untouched", function () {
var my_data = [{x: 1, y: 1}, {x: 2, y: 2}]; var my_data = [{x: 1, y: 1}, {x: 2, y: 2}];
var expected_data = [{x: 1, y: 1}, {x: 2, y: 2}]; var expected_data = [{x: 1, y: 1}, {x: 2, y: 2}];
Morris.Line({ Morris.Line({
@ -24,6 +24,18 @@
deepEqual(my_data, expected_data); deepEqual(my_data, expected_data);
}); });
test("Nicer error when the placeholder was not found.", function () {
var my_data = [{x: 1, y: 1}, {x: 2, y: 2}];
raises(function() {
Morris.Line({
element: "thisplacedoesnotexist",
data: my_data,
xkey: 'x',
ykeys: ['y'],
labels: ['dontcare']})
}, "Graph placeholder not found.");
});
test("Morris.commas", function () { test("Morris.commas", function () {
// zero // zero
equal(Morris.commas(0), "0", "commas(0) = 0") equal(Morris.commas(0), "0", "commas(0) = 0")