ksi*_*ndi 2 javascript jquery jeditable
解
感谢Arman的P.概念验证,终于让它与我的网站一起工作了.
码
//Edit Note
$(function(){
function makeEditable() {
$(".edit").editable('ajax/save.php?editnotetext', {
type : 'mce',
submit : '<button class="save_button">Save</button>',
event: 'dblclick',
indicator : 'Saving...',
tooltip : 'Doubleclick to edit...',
onblur: 'ignore',
width : '700px',
height : '100px',
callback : function(value, settings){
console.log('unlocked');
$.post('ajax/save.php?unlocknotetext', {"id" : $(this).attr('id')});
$(this).effect("highlight", {}, 3000);
$(this).parents('.panel').effect("highlight", {}, 3000);
},
'onreset' : function(){
console.log('unlocked');
$.post('ajax/save.php?unlocknotetext', {"id" : $(this).attr('id')});
}
});
};
makeEditable();
$('.edit').live('click', function() {
console.log('locked');
$.post('ajax/save.php?locknotetext', {"id" : $(this).attr('id')});
});
$(".edit").click(function() {
$.post('ajax/save.php?checklock', {"id" : $(this).attr('id')},
function(data) {
// USE SAME OPTION IN BOTH PLACES
// IF YOU USE 1ST OPTION, YOU CAN TAKE JEDITABLE OUT OF makeEditable() and do everything without that function
if (data[0].is_locked == 1) {
// Option 1
//$(this).editable('disable');
//alert(data[0].username.toUpperCase() + " is editing this note!");
// Option 2
$(".edit").unbind('dblclick');
} else {
// Option 1
//$(this).editable('enable')
// Option 2
makeEditable();
}
},
"json"
);
});
});
Run Code Online (Sandbox Code Playgroud)
所以现在,根据我认为Arman的建议,如果触发自定义事件,我只能使JEdtiable工作.我只是在找不到锁定时才试图触发事件.但现在JEditable没有被召唤.我试图触发它的方式有些不对劲.[注意:如果,如果我有一个按钮样的测试是JEditable不会被调用<button onclick="$('.edit').trigger('custom_event');">Click to Edit</button>]
码
所以这是我的新代码
$(function(){
$(".edit").bind("dblclick",function() {
$.ajax({ // first check to see if locked
type: "POST",
url: "ajax/save.php?locknotetext",
data: {"id" : $(this).attr('id')},
dataType: "json",
success: function(data){
if (data[0].is_locked == 1){ // if locked then alert someone is editing ntoe
alert(data[0].username.toUpperCase() + " is editing this note!");
}
else{
$(this).trigger('custom_event');
$(this).unbind('custom_event.editable');
}
}
}); //close $.ajax(
});
});
Run Code Online (Sandbox Code Playgroud)
这是JEditable的作品
$(function(){
$(".edit").editable('ajax/save.php?editnotetext', {
type : 'mce',
submit : '<button class="save_button">Save</button>',
event: 'custom_event',
indicator : 'Saving...',
tooltip : 'Doubleclick to edit...',
onblur: 'ignore',
width : '700px',
height : '100px',
callback : function(value, settings){
console.log(this, value, settings);
$(this).effect("highlight", {}, 3000);
$(this).parents('.panel').effect("highlight", {}, 3000);
$.ajax({
type: "POST",
url: "ajax/save.php?unlocknotetext",
data: {"id" : $(this).attr('id')}
}); //close $.ajax(
//$(this).addClass("edit");
}
// },
// function(value, settings) {
// $(this).unbind('settings.event');
});
});
Run Code Online (Sandbox Code Playgroud)
背景
我正在创建一个人们可以共享和编辑笔记的网站.我想要做的是,如果某人正在编辑一个笔记,则该笔记会被锁定,以便其他用户无法编辑该笔记.
我正在使用JEditable.用户可以双击以编辑注释.
如果用户双击,我会进行AJAX调用以查看注释中是否存在锁定.如果没有,那么我锁定笔记,用户可以编辑.如果有锁定,我会提醒用户"userX当前正在编辑注释".
问题
我的问题是我只想在没有锁的情况下调用JEditable.否则,我只想提醒用户其他人正在编辑它.我在下面的代码中遇到的问题是无论如何都会调用JEditable.我还尝试使用另一个类名进行可编辑,只有在第一次AJAX调用的回调中没有锁时才添加类,但这也不起作用.
我们欢迎所有的建议!
JEditable在这一点原生支持:
$.fn.editable = function(target, options) {
if ('disable' == target) {
$(this).data('disabled.editable', true);
return;
}
if ('enable' == target) {
$(this).data('disabled.editable', false);
return;
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效:
$(...).editable("disable")
$(...).editable("enable")
Run Code Online (Sandbox Code Playgroud)