Skip to content

Commit 5ea269e

Browse files
committed
Merge pull request #6 from quantum/master
No longer require an id but rather generate one if not present
2 parents 62c1829 + 14cad58 commit 5ea269e

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/zingchart-angularjs.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
'use strict';
33
angular.module('zingchart-angularjs', [] )
44
.directive('zingchart', [function(){
5+
var currentAutoId = 1;
6+
57
return {
68
restrict : 'EA',
79
scope : {
@@ -10,6 +12,17 @@
1012
zcRender : '='
1113
},
1214
controller : ['$scope', '$element', '$attrs', function($scope, $element, $attrs){
15+
var id;
16+
17+
// Get or generate id
18+
if(!$element.attr('id')){
19+
id = 'zingchart-auto-' + currentAutoId;
20+
currentAutoId++;
21+
$element.attr('id', id);
22+
} else {
23+
id = $element.attr('id');
24+
}
25+
1326
var initializing = {
1427
json : true,
1528
values :true
@@ -21,12 +34,12 @@
2134
}
2235
if($scope.zcValues){
2336
if(isMultiArray($scope.zcValues)){
24-
zingchart.exec($attrs.id, 'setseriesvalues', {
37+
zingchart.exec(id, 'setseriesvalues', {
2538
values : $scope.zcValues
2639
});
2740
}
2841
else{
29-
zingchart.exec($attrs.id, 'setseriesvalues', {
42+
zingchart.exec(id, 'setseriesvalues', {
3043
values : [$scope.zcValues]
3144
});
3245
}
@@ -64,18 +77,15 @@
6477
else{
6578
_json.type = ($attrs.zcType) ? $attrs.zcType : _json.type
6679
}
67-
zingchart.exec($attrs.id, 'setdata', {
80+
zingchart.exec(id, 'setdata', {
6881
data : _json
6982
});
7083
}
7184
},true);
7285

7386
}],
7487
link : function($scope, $element, $attrs){
75-
//Setup json :
76-
if(!$attrs.id){
77-
throw new Error('ZingChart-AngularJS : Attribute ID needed');
78-
}
88+
var id = $element.attr('id');
7989

8090
//Defaults
8191
var _json = {
@@ -128,7 +138,7 @@
128138
_json.data.type = ($attrs.zcType) ? $attrs.zcType : _json.data.type;
129139
_json.height = ($attrs.zcHeight) ? $attrs.zcHeight : _json.height;
130140
_json.width = ($attrs.zcWidth) ? $attrs.zcWidth : _json.width;
131-
_json.id = $attrs.id;
141+
_json.id = id;
132142

133143
//Set the box-model of the container element if the height or width are defined as 100%.
134144
if(_json.width === "100%" && !$element.width){

0 commit comments

Comments
 (0)