Skip to content

Commit

Permalink
implementation of jQuery deferreds for views in Home page charts.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisekelley committed Oct 25, 2011
1 parent 4e29a20 commit 6a13e9a
Show file tree
Hide file tree
Showing 9 changed files with 9,172 additions and 62 deletions.
133 changes: 78 additions & 55 deletions _attachments/app/app.js
Expand Up @@ -90,23 +90,45 @@ var AppRouter = Backbone.Router.extend({
viewDiv.setAttribute("id", "homePageView");
$("#views").append(viewDiv);
}
var searchResults = new IncidentsList();
searchResults.db["keys"] = null;
searchResults.db["view"] = ["byIncidentSorted?descending=true&limit=15"];
searchResults.fetch({
success : function(){
FORMY.Incidents = searchResults;
//console.log("render; Incidents count: " + FORMY.Incidents.length);
var page = new Page({content: "Default List of Incidents:"});
(new HomeView({model: page, el: $("#homePageView")})).render();
//console.log("starting stripeme.");
$(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
$(".stripeMe tr:even").addClass("alt");
},
error : function(){
console.log("Error loading PatientRecordList: " + arguments);
}
});

// charts

// var report = new ReportCollection();
// report.db["keys"] = null;
// report.db["view"] = ["byDepartmentEducation"];
// Initialize the Collection
var reportCollectionInstance = new ReportCollection();


var searchResults = new IncidentsList();
searchResults.db["keys"] = null;
searchResults.db["view"] = ["byIncidentSorted?descending=true&limit=15"];
searchResults.fetch({
success : function(){
FORMY.Incidents = searchResults;
//console.log("render; Incidents count: " + FORMY.Incidents.length);
var page = new Page({content: "Default List of Incidents:"});
(new HomeView({model: page, el: $("#homePageView"), reportCollection: reportCollectionInstance})).render();
//console.log("starting stripeme.");
$(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
$(".stripeMe tr:even").addClass("alt");
},
error : function(){
console.log("Error loading PatientRecordList: " + arguments);
}
});


// $.when( education(reportDate, departmentReport)).done(function(){
// console.log( 'I fire once BOTH ajax requests have completed!' + JSON.stringify(departmentReport["education"]));
//
// bulletChart(departmentReport.education);
// rendercharts();
// })
// .fail(function(){
// console.log( 'I fire if one or more requests failed.' );
// });

},
search: function (searchTerm) {
console.log("search route.");
Expand Down Expand Up @@ -505,47 +527,48 @@ var AppRouter = Backbone.Router.extend({
Backbone.history.start();
FORMY.Incidents = new IncidentsList();

// charts
var departmentReport = new Object({date:null,education:null,health: null,finance:null});
var reportDate = new Date();
var reportYear = reportDate.getFullYear();
var reportMonth = reportDate.getMonth() + 1;
console.log("Generating report for: " + reportMonth + "/" + reportYear);
$.couch.db(Backbone.couch_connector.config.db_name).view("coconut/byDepartmentEducation", {
'reduce':true,
'group_level':2,
success: function(countData) {
function education (reportDate, departmentReport) {
var reportYear = reportDate.getFullYear();
var reportMonth = reportDate.getMonth() + 1;
var dfd = $.Deferred();

//var element = $("#chartimg");
var values = [];
var labels = [];
var indices = [];
var months = [];

//var counts = [];
for (i in countData.rows) {
console.log(countData.rows[i].key.join('-') + ": " + "countData.rows[i].value: " + JSON.stringify(countData.rows[i].value));
//values.push(data.rows[i].value.resolved);
labels.push(countData.rows[i].key.join('-'));
var year = parseInt(countData.rows[i].key[0], 10);
var month = parseInt(countData.rows[i].key[1], 10);
if ((year === reportYear) && (month === reportMonth)) {
values.push(countData.rows[i].value);
}
months.push(month);
indices.push(i);
}
console.log("labels: " + JSON.stringify(labels));
console.log("values: " + JSON.stringify(values));
console.log("months: " + JSON.stringify(months));
console.log("indices: " + JSON.stringify(indices));

bulletChart(values);
$.couch.db(Backbone.couch_connector.config.db_name).view("coconut/byDepartmentEducation", {
'reduce':true,
'group_level':2,
success: function(countData) {

//var element = $("#chartimg");
var values = [];
var labels = [];
var indices = [];
var months = [];

//var counts = [];
for (i in countData.rows) {
console.log(countData.rows[i].key.join('-') + ": " + "countData.rows[i].value: " + JSON.stringify(countData.rows[i].value));
//values.push(data.rows[i].value.resolved);
labels.push(countData.rows[i].key.join('-'));
var year = parseInt(countData.rows[i].key[0], 10);
var month = parseInt(countData.rows[i].key[1], 10);
if ((year === reportYear) && (month === reportMonth)) {
values.push(countData.rows[i].value);
}
months.push(month);
indices.push(i);
}
console.log("labels: " + JSON.stringify(labels));
console.log("values: " + JSON.stringify(values));
console.log("months: " + JSON.stringify(months));
console.log("indices: " + JSON.stringify(indices));
//bulletChart(values);
departmentReport.education = values;
//return departmentReport;
dfd.resolve;
}
}
}
);
rendercharts();
);
return dfd.promise();
};

//}
//}, 100);
2 changes: 2 additions & 0 deletions _attachments/app/models/Report.js
@@ -0,0 +1,2 @@
var Report = Backbone.Model.extend({
});
32 changes: 32 additions & 0 deletions _attachments/app/models/ReportCollection.js
@@ -0,0 +1,32 @@
var ReportCollection = Backbone.Collection.extend({
model: Report,
//url: '/reportElements',
// initialize: function(){
// // Assign the Deferred issued by fetch() to the Collection
// this.deferred = this.fetch();
// },
db : {
view: "byDepartmentEducation?reduce=true&group_level=2",
keys: null,
},
parse: function(response) {
console.log("parse:" + JSON.stringify(response));
return response;
},
initialize: function(){
// Assign the Deferred issued by fetch() to the Collection
console.log("ReportCollection init.");
this.deferred = this.fetch({
success : function(countData){
FORMY.Report = countData;
console.log("Report count: " + FORMY.Report.length);
console.log("this from ReportCollection init: " + JSON.stringify(this.deferred));
},
error : function(){
console.log("Error loading Report: " + arguments);
}
});
console.log("ReportCollection deferred.");
//this.deferred = this.fetch();
}
});
64 changes: 61 additions & 3 deletions _attachments/app/views/HomeView.js
Expand Up @@ -51,6 +51,7 @@ var HomeView = Backbone.View.extend({
FORMY.router.navigate('search/' + searchTerm, true);
},
orientation: "horiz",
reportCollection:null,
render: function() {
// $("#formRenderingView").remove();
// $("#recordView").remove();
Expand All @@ -72,16 +73,73 @@ var HomeView = Backbone.View.extend({
//this.template = loadTemplate("home.template.html");
}

$.when(this.options.reportCollection.deferred)
.then(function(countData){
console.log( 'I fire once BOTH ajax requests have completed!' );

var departmentReport = new Object({date:null,education:null,health: null,finance:null});
var reportDate = new Date();
var reportYear = reportDate.getFullYear();
var reportMonth = reportDate.getMonth() + 1;

console.log("Generating report for: " + reportDate);

var values = [];
var labels = [];
var indices = [];
var months = [];

//var counts = [];
for (i in countData.rows) {
console.log(countData.rows[i].key.join('-') + ": " + "countData.rows[i].value: " + JSON.stringify(countData.rows[i].value));
//values.push(data.rows[i].value.resolved);
labels.push(countData.rows[i].key.join('-'));
var year = parseInt(countData.rows[i].key[0], 10);
var month = parseInt(countData.rows[i].key[1], 10);
if ((year === reportYear) && (month === reportMonth)) {
values.push(countData.rows[i].value);
}
months.push(month);
indices.push(i);
}
console.log("labels: " + JSON.stringify(labels));
console.log("values: " + JSON.stringify(values));
console.log("months: " + JSON.stringify(months));
console.log("indices: " + JSON.stringify(indices));
//bulletChart(values);
departmentReport.education = values;

bulletChart(departmentReport.education);
rendercharts();


})
.fail(function(){
console.log( 'I fire if one or more requests failed.' );
});

// this.options.reportCollection.deferred.done(function(countData){
//// var homeViewHtml = this.template(this.model.toJSON());
//// console.log("rendering HomeView");
//// //$(this.el).html(homeViewHtml);
//// $("#homePageView").html(homeViewHtml);
//// //if(FORMY.Incidents.length > 0){
//// FORMY.Incidents.each(this.addOne);
//
//
// //}
//
// });

var homeViewHtml = this.template(this.model.toJSON());
console.log("rendering HomeView");
//$(this.el).html(homeViewHtml);
$("#homePageView").html(homeViewHtml);
//if(FORMY.Incidents.length > 0){
FORMY.Incidents.each(this.addOne);

//}

$(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
$(".stripeMe tr:even").addClass("alt");
$(".stripeMe tr:even").addClass("alt");
return this;
},
});
Expand Down
4 changes: 3 additions & 1 deletion _attachments/index.html
Expand Up @@ -58,7 +58,7 @@

</body>
<!-- TODO for deployment - combine & minify all of this -->
<script type="text/javascript" src="js-libraries/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js-libraries/jquery-1.6.2.js"></script>
<script type="text/javascript" src="js-libraries/jquery-ui-1.8.15.custom.min.js"></script>

<script type="text/javascript" src="js-libraries/underscore-min.js"></script>
Expand Down Expand Up @@ -121,6 +121,8 @@
<script type="text/javascript" src="app/models/IncidentRecordList.js"></script>
<script type="text/javascript" src="app/views/IncidentView.js"></script>
<script type="text/javascript" src="app/views/RecordView.js"></script>
<script type="text/javascript" src="app/models/Report.js"></script>
<script type="text/javascript" src="app/models/ReportCollection.js"></script>
<script type="text/javascript" src="app/coco-charts.js"></script>
<script type="text/javascript" src="app/app.js"></script>
<script type="text/javascript" src="app/coconut-utils.js"></script>
Expand Down
11 changes: 10 additions & 1 deletion _attachments/js-libraries/backbone-couchdb.js
Expand Up @@ -79,9 +79,18 @@
var doc, _i, _len, _ref, _temp;
_temp = [];
_ref = data.rows;
console.log("data: " + JSON.stringify(data));
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
doc = _ref[_i];
_temp.push(doc.value);
var value = doc.value;
if (typeof(value) != 'object') {
console.log("Not object: " + value + " doc.key: " + doc.key);
doc["_id"] = doc.key;
_temp.push(doc);
} else {
_temp.push(doc.value);
}

}
return opts.success(_temp);
}, this),
Expand Down
2 changes: 2 additions & 0 deletions _attachments/js-libraries/backbone.js
Expand Up @@ -202,6 +202,7 @@
// Run validation.
if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;

console.log("attrs: " + JSON.stringify(attrs));
// Check for changes of `id`.
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];

Expand Down Expand Up @@ -522,6 +523,7 @@
var collection = this;
var success = options.success;
options.success = function(resp, status, xhr) {
console.log("resp: " + JSON.stringify(resp));
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
if (success) success(collection, resp);
};
Expand Down

0 comments on commit 6a13e9a

Please sign in to comment.