From 68c487b91d49de0b757c9b93926bbf27dc7694b3 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 12 Jan 2013 19:10:43 +0100 Subject: [PATCH] Added resize handler to resize the chart when resizing the window, added global ajax error handler. --- src/html/js/entries.js | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/html/js/entries.js b/src/html/js/entries.js index 80ad525..935cd69 100644 --- a/src/html/js/entries.js +++ b/src/html/js/entries.js @@ -41,6 +41,7 @@ var ListViewModel = function() { self.selectedItem = ko.observable(); self.savedItem = ko.observable(); self.itemToRemove = ko.observable(); + self.chart = null; self.entriesChart = ko.computed(function() { var entries = self.entries().slice().reverse(); @@ -355,18 +356,7 @@ ko.bindingHandlers.dateValue = { } }; -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 = function(entries, element) { if(entries && entries.length > 0) { var firstDate, lastDate; var chartValues = [[], []]; @@ -388,7 +378,7 @@ ko.bindingHandlers.chart = { $(element).html(""); // plot chart - $.jqplot(element.id, chartValues, { + jqplot = $.jqplot(element.id, chartValues, { axes:{ xaxis:{ renderer:$.jqplot.DateAxisRenderer, @@ -419,12 +409,33 @@ ko.bindingHandlers.chart = { //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); $(viewModel.loadAccounts);