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 addCategory(description) { function clearCategories() {
description = description.trim(); $('#categories').empty();
if (!description) {
return;
} }
categories[guid()] = {description: description, value: 0}; function addCategory(description) {
$.getJSON('/learn', {description: description}, function(results) {
if (results.success) {
var categories = {};
categories[results.id] = {description: results.description, value: 0};
displayCategories(categories);
}
});
}
transmitCategories(); function refreshCategories() {
displayCategories(); $.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
};
}
clearCategories();
displayCategories(categories);
});
} }
function onReady() { function onReady() {
@ -64,19 +72,11 @@
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();
});
} }
$(document).on({ $(document).on({

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();
if (description) {
pool.query('INSERT INTO categories(description, id) VALUES(?, ?)', [description, id], function(err, rows) { pool.query('INSERT INTO categories(description, id) VALUES(?, ?)', [description, id], function(err, rows) {
if (err) { if (err) {
throw 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) {