function lightswitch()
{
    id = this.id.replace("light_", "");
    $.post("/lightswitch", { id: id });
};

$(document).ready(function () {
    $(".toggle").each(function(index) {
        obj = $(".toggle")[index];
        document.querySelector('#' + obj.id).addEventListener('toggle', lightswitch, obj.id);
    });
});

function lightswitch()
{
    id = this.id.replace("light_", "");
    $.post("/lightswitch", { id: id })
        .fail(location.reload());
};

$(document).ready(function () {
    $(".toggle").each(function(index) {
        obj = $(".toggle")[index];
        document.querySelector('#' + obj.id).addEventListener('toggle', lightswitch, obj.id);
    });
});

Handle failure to toggle light
Handle failure to toggle light

Handle it by reloading the entire page. A little crude, sure, but it
does at least work.
