在data-*属性中成功回调x可编辑编码

X-editable encoding success callback in data-* attribute

本文关键字:编辑 编码 回调 成功 data- 属性      更新时间:2023-09-26

我正在使用X-editable创建可以通过ajax调用就地编辑的链接。

我的页面上所有可编辑的都是用类edititable创建的,并使用data-*参数将值传递给x-editable。总的来说,这是可行的,因为更新提交得很好。

然而,在下面的例子中,data-success函数从来没有调用过——在这种情况下,我从来没有看到控制台日志。

<a href="#"  
class="editable" 
data-type="select" 
data-send="always" 
data-source='{"Windows 2000":"Windows 2000","Windows Server 2003":"Windows Server 2003","Windows Server 2008":"Windows Server 2008","Windows Server 2008 R2":"Windows Server 2008 R2","Windows Server 2012":"Windows Server 2012","Windows Server 2012 R2":"Windows Server 2012 R2","Other":"Other"}' data-name="Value"  
data-pk="1155"
data-display="function (value, response) { return false;}"
data-success="function (response, newValue) { console.log('success');}"
data-url="/attribute/updateField"
data-title="Title info">Windows Server 2008</a>

我问过x -edit的程序员,因为我也在尝试同样的事情,他回答说这是不可能的。他的回答:

我认为这是不可能的,因为数据属性被解析,但不像javascript那样eval。

我也认为更好的方法是将js逻辑存储在js文件中,而不是在html属性中。

我找到的最干净的解决方案是:

HTML:

<a href="#"  
class="editable" 
data-type="select" 
data-send="always" 
data-source='{"Windows 2000":"Windows 2000","Windows Server 2003":"Windows Server 2003","Windows Server 2008":"Windows Server 2008","Windows Server 2008 R2":"Windows Server 2008 R2","Windows Server 2012":"Windows Server 2012","Windows Server 2012 R2":"Windows Server 2012 R2","Other":"Other"}' data-name="Value"  
data-pk="1155"
data-eval-display="console.log('display', response, newValue);"
data-eval-success="console.log('success', response, newValue);"
data-url="/attribute/updateField"
data-title="Title info">Windows Server 2008</a>
JavaScript:

$('.editable').editable({
    display: function(response, newValue) {
        eval($(this).data('eval-display'));
    },
    success: function(response, newValue) {
        eval($(this).data('eval-success'));
    }
});