It seems my question has become a tumbleweed and I also don't need an answer anymore. I've solved this on my own by changing my approach. Instead of doing a direct inline edit when the user clicks on the #body or #title I've instead added an edit "button" (really a <span>
) which toggles between edit/non-edit mode. Here's the new approach I took which ended up working really well:
On the page load I set up the edit "button":
$(function () {
$('.edit').click(edit);
});
I set up the edit click event like this:
function edit() {
//Make the title and header look editable
$('#title').css({
border: "1px dotted #333"
});
$('#body').css({
border: "1px dotted #333"
});
//Detatch the 'edit' handler
$('.edit').unbind();
//Change it to a 'save' event
$('.edit').html('Save').click(save);
//Go into edit mode
//Prevent adding the editor over and over
if (!tinymce.activeEditor) {
tinymce.init({
selector: "#title",
theme: "modern",
language: "en",
inline: true,
toolbar: "undo redo",
menubar: false,
browser_spellcheck: true
});
tinymce.init({
selector: "#body",
theme: "modern",
language: "en",
inline: true,
menubar: false,
browser_spellcheck: true,
plugins: [
"link"
],
toolbar: "undo redo | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link"
});
}
}
While in edit mode the button becomes a 'save'. The save event sends an asyn Json response to the controller and is handled like this:
function save() {
//Make the title and header look non-editable
$('#title').css({
border: "none"
});
$('#body').css({
border: "none"
});
//Detatch the 'edit' handler
$('.edit').unbind();
//Change it to a 'edit' event
$('.edit').html('Edit').click(edit);
//Disable edit mode
//Only remove the current editor if it's active
if (tinymce.activeEditor) {
tinymce.triggerSave();
tinymce.execCommand('mceRemoveEditor', true, 'title');
tinymce.execCommand('mceRemoveEditor', true, 'body');
}
//Persist to DB
try {
var title = $('#title').html();
var body = $('#body').html();
$.ajax({
url: Routings.TopicSave,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
ID: parseInt(TopicID, 10),
Title: title,
Body: body
})
});
} catch (err) {
alert(err);
}
}
I've omitted the "cancel" functionality example as it doesn't add any value to this question, but if you're interested you can see an example here: Handle discard changes with TinyMCE