Allow displaying and adding of categories via ajax
This commit is contained in:
parent
6b815840c7
commit
ff099ddb1c
@ -23,40 +23,48 @@
|
||||
(function(categories) {
|
||||
'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() {
|
||||
console.log(categories);
|
||||
}
|
||||
|
||||
function displayCategories() {
|
||||
function displayCategories(categories) {
|
||||
var template = Handlebars.compile($('#template').html());
|
||||
|
||||
$('#categories').empty();
|
||||
$('#categories').append(template({categories: categories}));
|
||||
|
||||
$('#categories input:radio').change(function() {
|
||||
categories[$(this).attr('categoryId')].value = parseInt(this.value);
|
||||
transmitCategories();
|
||||
});
|
||||
}
|
||||
|
||||
function addCategory(description) {
|
||||
description = description.trim();
|
||||
if (!description) {
|
||||
return;
|
||||
function clearCategories() {
|
||||
$('#categories').empty();
|
||||
}
|
||||
|
||||
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();
|
||||
displayCategories();
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
clearCategories();
|
||||
displayCategories(categories);
|
||||
});
|
||||
}
|
||||
|
||||
function onReady() {
|
||||
@ -64,19 +72,11 @@
|
||||
return new Handlebars.SafeString(value == this.value ? 'checked' : '');
|
||||
});
|
||||
|
||||
$.getJSON('/query', _ctx.query, function(results) {
|
||||
var profile = {};
|
||||
for (var i = 0, length = results.length; i < length; ++i) {
|
||||
var result = results[i];
|
||||
profile[result.id] = {description: result.description, value: 0};
|
||||
}
|
||||
refreshCategories();
|
||||
|
||||
$('#addCategory').click(function() {
|
||||
addCategory($('#newCategory').val());
|
||||
});
|
||||
|
||||
displayCategories();
|
||||
});
|
||||
}
|
||||
|
||||
$(document).on({
|
||||
|
@ -225,16 +225,25 @@ function getCategories(callback) {
|
||||
}
|
||||
|
||||
function addCategory(query, callback) {
|
||||
var description = query.description;
|
||||
var description = query.description.trim();
|
||||
var id = uuid.v1();
|
||||
|
||||
if (description) {
|
||||
pool.query('INSERT INTO categories(description, id) VALUES(?, ?)', [description, id], function(err, rows) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
callback({id: id, description: description});
|
||||
callback({
|
||||
id: id,
|
||||
description: description,
|
||||
success: true
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
callback({success: false});
|
||||
}
|
||||
}
|
||||
|
||||
function execQuery(query, callback) {
|
||||
|
Loading…
Reference in New Issue
Block a user