如何防止标签被删除?

How to prevent a tag from deletion?

我正在使用 twitter typeaheadjs 在我的页面上填充标签。当标签值等于登录 ID 时,我想防止标签被删除。

我从 docs 那里得到了事件:

项目删除事件之前:

$('input').on('beforeItemRemove', function(event) {
    alert("before item removed");
    var tag = event.item;
    if(tag == DataMixin.data.user.username){
        console.log('cannot delete agent');
    }else{
        console.log('agent deleted');
    }
 });

项目删除事件后:

$('input').on('itemRemoved', function (event) {
    alert("item removed");
        }
    }
});

如何防止在 typeaheadJS 中基于上述条件删除标签

来自文档:

event.cancel: set to true to prevent the item getting removed

$('input').on('beforeItemRemove', function(event) {
    alert("before item removed");
    var tag = event.item;
    if(tag == DataMixin.data.user.username){
        event.cancel = true;
        console.log('cannot delete agent');
    }else{
        console.log('agent deleted');
    }
 });