experimenting with custom handler
This commit is contained in:
parent
2e51aff4be
commit
9a2c8cc712
@ -3,7 +3,8 @@ import json_params, capture_errors_json, yield_error from require "lapis.applica
|
||||
import assert_valid from require "lapis.validate"
|
||||
|
||||
import APIKeys, Users, Tasks from require "models"
|
||||
-- import locate from require "locator"
|
||||
import autoload from require "locator"
|
||||
import api_request from autoload "helpers"
|
||||
-- import random from locate "calc"
|
||||
-- import escape_similar_to from locate "db"
|
||||
|
||||
@ -27,7 +28,7 @@ class API extends Application
|
||||
[err_test: "/err"]: =>
|
||||
error "this is a testing error"
|
||||
|
||||
[new: "/new"]: capture_errors_json json_params =>
|
||||
[new: "/new"]: api_request =>
|
||||
assert_valid @params, {
|
||||
{"content", exists: true, min_length: 1, "Task content not specified."}
|
||||
}
|
||||
|
16
helpers/api.moon
Normal file
16
helpers/api.moon
Normal file
@ -0,0 +1,16 @@
|
||||
import json_params, capture_errors from require "lapis.application"
|
||||
import remove from table
|
||||
|
||||
api_request = (fn) ->
|
||||
json_params capture_errors {
|
||||
fn,
|
||||
on_error: =>
|
||||
status = 400
|
||||
if "number" == type @errors[1]
|
||||
status = remove(@errors, 1)
|
||||
return(:status, json: { errors: @errors })
|
||||
}
|
||||
|
||||
{
|
||||
:api_request
|
||||
}
|
@ -20,15 +20,20 @@ function new_task() {
|
||||
$.post("/v1/new", {api_key: API_KEY, content: input.val()}, function(data) {
|
||||
let input = $("input", template);
|
||||
input.prop("id", "task-" + data.task.id);
|
||||
// input.prop("onchange", "check('" + data.task.id + "')");
|
||||
input.change(function() {
|
||||
check(data.task.id);
|
||||
});
|
||||
input.after(" " + data.task.content);
|
||||
$("#new-task").before(template);
|
||||
})
|
||||
.fail(function(request) {
|
||||
// TODO handle error
|
||||
let input = $("input", template);
|
||||
input.after("ERROR:" + request.responseJSON.errors.join(" "))
|
||||
.css("background-color", "red")
|
||||
.css("color", "white");
|
||||
input.remove();
|
||||
})
|
||||
.always(function() {
|
||||
$("#new-task").before(template);
|
||||
});
|
||||
input.val("");
|
||||
return false; // prevent form submission
|
||||
|
Loading…
Reference in New Issue
Block a user