1

Allow displaying and adding of categories via ajax

This commit is contained in:
Alex Yatskov 2015-03-03 18:52:33 +09:00
parent 6b815840c7
commit ff099ddb1c
2 changed files with 45 additions and 36 deletions

View File

@ -23,40 +23,48 @@
(function(categories) { (function(categories) {
'use strict'; 'use strict';
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
function transmitCategories() { function transmitCategories() {
console.log(categories); console.log(categories);
} }
function displayCategories() { function displayCategories(categories) {
var template = Handlebars.compile($('#template').html()); var template = Handlebars.compile($('#template').html());
$('#categories').empty();
$('#categories').append(template({categories: categories})); $('#categories').append(template({categories: categories}));
$('#categories input:radio').change(function() { $('#categories input:radio').change(function() {
categories[$(this).attr('categoryId')].value = parseInt(this.value); categories[$(this).attr('categoryId')].value = parseInt(this.value);
transmitCategories(); transmitCategories();
}); });
} }
function clearCategories() {
$('#categories').empty();
}
function addCategory(description) { function addCategory(description) {
description = description.trim(); $.getJSON('/learn', {description: description}, function(results) {
if (!description) { if (results.success) {
return; var categories = {};
} categories[results.id] = {description: results.description, value: 0};
displayCategories(categories);
}
});
}
categories[guid()] = {description: description, value: 0}; function refreshCategories() {
$.getJSON('/categories', function(results) {
var categories = {};
for (var i = 0, length = results.length; i < length; ++i) {
var result = results[i];
categories[result.id] = {
description: result.description,
value: 0
};
}
transmitCategories(); clearCategories();
displayCategories(); displayCategories(categories);
});
} }
function onReady() { function onReady() {
@ -64,18 +72,10 @@
return new Handlebars.SafeString(value == this.value ? 'checked' : ''); return new Handlebars.SafeString(value == this.value ? 'checked' : '');
}); });
$.getJSON('/query', _ctx.query, function(results) { refreshCategories();
var profile = {};
for (var i = 0, length = results.length; i < length; ++i) {
var result = results[i];
profile[result.id] = {description: result.description, value: 0};
}
$('#addCategory').click(function() { $('#addCategory').click(function() {
addCategory($('#newCategory').val()); addCategory($('#newCategory').val());
});
displayCategories();
}); });
} }

View File

@ -225,16 +225,25 @@ function getCategories(callback) {
} }
function addCategory(query, callback) { function addCategory(query, callback) {
var description = query.description; var description = query.description.trim();
var id = uuid.v1(); var id = uuid.v1();
pool.query('INSERT INTO categories(description, id) VALUES(?, ?)', [description, id], function(err, rows) { if (description) {
if (err) { pool.query('INSERT INTO categories(description, id) VALUES(?, ?)', [description, id], function(err, rows) {
throw err; if (err) {
} throw err;
}
callback({id: id, description: description}); callback({
}); id: id,
description: description,
success: true
});
});
}
else {
callback({success: false});
}
} }
function execQuery(query, callback) { function execQuery(query, callback) {