The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
[% DEFAULT
    cat = 'model'  # category
-%]
// !!! DO NOT EDIT !!!
// This .js file was generated automatically from handlers.tt

function get[% cat | ucfirst %]s (opts) {
    setStatus(true, 'get[% cat | ucfirst %]');
    openresty.callback = render[% cat | ucfirst %]s;
    openresty.get('/=/[% cat %]');
    get[% cat | ucfirst %]Menu();
}

function render[% cat | ucfirst %]s (res) {
    setStatus(false, 'get[% cat | ucfirst %]s');
    if (!openresty.isSuccess(res)) {
        error("Failed to get [% cat %] list: " + res.error);
        return;
    }
    $("#main").html(
        Jemplate.process(
            'object-list.tt',
            { objects: res, category: '[% cat %]' }
        )
    ).postprocess();
}

function get[% cat | ucfirst %]Menu () {
    setStatus(true, 'get[% cat | ucfirst %]Menu');
    openresty.callback = render[% cat | ucfirst %]Menu;
    openresty.get('/=/[% cat %]');
}

function render[% cat | ucfirst %]Menu (res) {
    setStatus(false, 'get[% cat | ucfirst %]Menu');
    if (!openresty.isSuccess(res)) {
        error("Failed to get the [% cat %] menu: " + res.error);
        return;
    }
    $("#menu").html(
        Jemplate.process(
            'menu.tt',
            { active_item: '[% cat | ucfirst %]s', submenu: res }
        )
    ).postprocess();
    $("#logout-link").click(logout);
}

function get[% cat | ucfirst %] (name) {
    setStatus(true, 'get[% cat | ucfirst %]');
    openresty.callback = function (res) { render[% cat | ucfirst %](res, name) };
    openresty.get('/=/[% cat %]/' + name);
}

function render[% cat | ucfirst %] (res, name) {
    setStatus(false, 'get[% cat | ucfirst %]');
    if (!openresty.isSuccess(res)) {
        error("Failed to get [% cat %] " + name + ": " + res.error);
        return;
    }
    if ($("#menu").find("#[% cat %]-" + name).length == 0) {
        //error("[% cat %]-" + name + " not found!");
        get[% cat | ucfirst %]Menu();
    }
    $("#main").html(
        Jemplate.process(
            '[% cat %].tt',
            { [% cat %]: res }
        )
    ).postprocess();
}

function delete[% cat | ucfirst %] ([% cat %], nextPage) {
    if (!confirm("Are you sure to delete [% cat %] " + [% cat %] + "?"))
        return;
    setStatus(true, 'delete[% cat | ucfirst %]');
    openresty.callback = function (res) {
        afterDelete[% cat | ucfirst %](res, [% cat %], nextPage);
    };
    openresty.del('/=/[% cat %]/' + [% cat %]);
}

function afterDelete[% cat | ucfirst %] (res, [% cat %], nextPage) {
    setStatus(false, 'delete[% cat | ucfirst %]');
    if (!openresty.isSuccess(res)) {
        error("Failed to delete [% cat %] " + [% cat %] + ": " + res.error);
        return;
    }
    gotoNextPage(nextPage);
}