Added resize handler to resize the chart when resizing the window, added global ajax error handler.

This commit is contained in:
Alexis Lahouze 2013-01-12 19:10:43 +01:00
parent d044300680
commit 68c487b91d

View File

@ -41,6 +41,7 @@ var ListViewModel = function() {
self.selectedItem = ko.observable(); self.selectedItem = ko.observable();
self.savedItem = ko.observable(); self.savedItem = ko.observable();
self.itemToRemove = ko.observable(); self.itemToRemove = ko.observable();
self.chart = null;
self.entriesChart = ko.computed(function() { self.entriesChart = ko.computed(function() {
var entries = self.entries().slice().reverse(); var entries = self.entries().slice().reverse();
@ -355,18 +356,7 @@ ko.bindingHandlers.dateValue = {
} }
}; };
ko.bindingHandlers.chart = { drawChart = function(entries, element) {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
// empty - left as placeholder if needed later
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
//var entries = dataSource ? unwrap(dataSource) : null;
var entries = dataSource ? unwrap(dataSource) : null;
if(entries && entries.length > 0) { if(entries && entries.length > 0) {
var firstDate, lastDate; var firstDate, lastDate;
var chartValues = [[], []]; var chartValues = [[], []];
@ -388,7 +378,7 @@ ko.bindingHandlers.chart = {
$(element).html(""); $(element).html("");
// plot chart // plot chart
$.jqplot(element.id, chartValues, { jqplot = $.jqplot(element.id, chartValues, {
axes:{ axes:{
xaxis:{ xaxis:{
renderer:$.jqplot.DateAxisRenderer, renderer:$.jqplot.DateAxisRenderer,
@ -419,12 +409,33 @@ ko.bindingHandlers.chart = {
//candleStick: true //candleStick: true
}}], }}],
});//*/ });//*/
$(window).resize(function() {
jqplot.replot({resetAxes: true});
});
} }
};
ko.bindingHandlers.chart = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
// empty - left as placeholder if needed later
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
//var entries = dataSource ? unwrap(dataSource) : null;
var entries = dataSource ? unwrap(dataSource) : null;
drawChart(entries, element);
} }
}; };
var viewModel = new ListViewModel(); $(document).ajaxError(function(event, xhr, settings) {
message("error", "Error.", xhr.statusText);
});
var viewModel = new ListViewModel();
ko.applyBindings(viewModel); ko.applyBindings(viewModel);
$(viewModel.loadAccounts); $(viewModel.loadAccounts);