Created
November 24, 2011 19:52
-
-
Save glennblock/1392148 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TableQuery = require('./../../lib/azure').TableQuery; | |
module.exports = Home; | |
var uuid = require('node-uuid'); | |
function Home(client) { | |
this.client = client; | |
}; | |
Home.prototype = { | |
showResults: function (res, taskList) { | |
res.render('home.ejs', { | |
locals: { | |
title: 'Task list', | |
layout: false, | |
taskList: taskList | |
} | |
}); | |
}, | |
getItems: function (allItems, callback) { | |
var query = TableQuery.select().from('tasks'); | |
if (!allItems) { | |
query = query.where('completed eq ?', false); | |
} | |
this.client.queryEntities(query, callback); | |
}, | |
showItems: function (req, res) { | |
var self = this; | |
this.getItems(false, function (resp, taskList) { | |
if (!taskList) { | |
self.taskList = []; | |
} | |
self.showResults(res, taskList); | |
}); | |
}, | |
newItem: function (req, res) { | |
var self = this; | |
var createItem = function (resp, taskList) { | |
if (!taskList) { | |
self.taskList = []; | |
} | |
var item = req.body.item; | |
item.RowKey = uuid(); | |
item.PartitionKey = 'partition1'; | |
item.completed = false; | |
self.client.insertEntity('tasks', item, function () { | |
self.showItems(req, res); | |
}); | |
}; | |
this.getItems(true, createItem); | |
}, | |
markCompleted: function (req, res) { | |
var self = this; | |
var postedItems = req.body.completed; | |
if (!postedItems.forEach) | |
postedItems = [postedItems]; | |
var process = { | |
processNextItem: function (err) { | |
var item = postedItems.pop(); | |
if (item) process.getItemToUpdate(item); | |
else self.getItems(false, function (resp, taskitems) { | |
self.showResults(res, taskitems); | |
}); | |
}, | |
getItemToUpdate: function (item) { | |
self.client.queryEntity('tasks', 'partition1', item, process.updateItem); | |
}, | |
updateItem: function (resp, task) { | |
if (task) { | |
task.completed = true; | |
self.client.updateEntity('tasks', task, process.processNextItem); | |
} | |
} | |
}; | |
process.processNextItem(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment