Skip to content

Commit f827bac

Browse files
12 - Inline Js to External
1 parent fe47842 commit f827bac

File tree

3 files changed

+57
-65
lines changed

3 files changed

+57
-65
lines changed

src/analytics/templates/analytics/sales.html

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,6 @@
11
{% extends "base.html" %}
22

33

4-
{% block javascript %}
5-
6-
<script>
7-
8-
$(document).ready(function(){
9-
function renderChart(id, data, labels){
10-
// var ctx = document.getElementById("myChart").getContext('2d');
11-
var ctx = $('#' + id)
12-
var myChart = new Chart(ctx, {
13-
type: 'line',
14-
data: {
15-
labels: labels,
16-
datasets: [{
17-
label: 'Sales',
18-
data: data,
19-
backgroundColor: 'rgba(0, 158, 29, 0.45)',
20-
borderColor:'rgba(0, 158, 29, 1)',
21-
}]
22-
},
23-
options: {
24-
scales: {
25-
yAxes: [{
26-
ticks: {
27-
beginAtZero:true
28-
}
29-
}]
30-
},
31-
backgroundColor: 'rgba(75, 192, 192, 1)'
32-
}
33-
});
34-
}
35-
36-
function getSalesData(id, type){
37-
var url = '/analytics/sales/data/'
38-
var method = 'GET'
39-
var data = {"type": type}
40-
$.ajax({
41-
url: url,
42-
method: method,
43-
data: data,
44-
success: function(responseData){
45-
renderChart(id, responseData.data, responseData.labels)
46-
}, error: function(error){
47-
$.alert("An error occurred")
48-
}
49-
})
50-
}
51-
var chartsToRender = $('.cfe-render-chart')
52-
$.each(chartsToRender, function(index, html){
53-
var $this = $(this)
54-
if ( $this.attr('id') && $this.attr('data-type')){
55-
getSalesData($this.attr('id'), $this.attr('data-type'))
56-
}
57-
58-
})
59-
60-
})
61-
62-
63-
64-
</script>
65-
{% endblock %}
66-
67-
68-
694
{% block content %}
705

716

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
$(document).ready(function(){
3+
function renderChart(id, data, labels){
4+
// var ctx = document.getElementById("myChart").getContext('2d');
5+
var ctx = $('#' + id)
6+
var myChart = new Chart(ctx, {
7+
type: 'line',
8+
data: {
9+
labels: labels,
10+
datasets: [{
11+
label: 'Sales',
12+
data: data,
13+
backgroundColor: 'rgba(0, 158, 29, 0.45)',
14+
borderColor:'rgba(0, 158, 29, 1)',
15+
}]
16+
},
17+
options: {
18+
scales: {
19+
yAxes: [{
20+
ticks: {
21+
beginAtZero:true
22+
}
23+
}]
24+
},
25+
backgroundColor: 'rgba(75, 192, 192, 1)'
26+
}
27+
});
28+
}
29+
30+
function getSalesData(id, type){
31+
var url = '/analytics/sales/data/'
32+
var method = 'GET'
33+
var data = {"type": type}
34+
$.ajax({
35+
url: url,
36+
method: method,
37+
data: data,
38+
success: function(responseData){
39+
renderChart(id, responseData.data, responseData.labels)
40+
}, error: function(error){
41+
$.alert("An error occurred")
42+
}
43+
})
44+
}
45+
var chartsToRender = $('.cfe-render-chart')
46+
$.each(chartsToRender, function(index, html){
47+
var $this = $(this)
48+
if ( $this.attr('id') && $this.attr('data-type')){
49+
getSalesData($this.attr('id'), $this.attr('data-type'))
50+
}
51+
52+
})
53+
54+
})

src/templates/base/js.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<!-- Django Secure AJAX JS -->
2525
<script src='{% static "js/csrf.ajax.js" %}'></script>
2626

27+
<!-- eCommerce Sales Data JS -->
28+
<script src='{% static "js/ecommerce.sales.js" %}'></script>
29+
2730
<!-- eCommerce Custom JS -->
2831
<script src='{% static "js/ecommerce.js" %}'></script>
2932

0 commit comments

Comments
 (0)