Skip to content

Instantly share code, notes, and snippets.

@ringe
Created January 9, 2014 11:07
Show Gist options
  • Save ringe/8332578 to your computer and use it in GitHub Desktop.
Save ringe/8332578 to your computer and use it in GitHub Desktop.
Combine Trumbowyg with RestInPlace. I had some difficulties extending RestInPlaceEditor.forms with the "trumbowyg" type. Mainly because the callbacks are not documented in Trumbowyg, I believe. I was not able to use the RestInPlace method _this.update(); but had to copy the code from restinplace.js and implement a custom ajax handling. This leav…
<div id=page_1 data-url=/pages/1><>
<h1 class="rest-in-place" data-attribute="title">Nord-Norges stØrste næringsmagasin</h1>
<div id=pagebody_1 class="pagebody" data-attribute="body" data-formtype="trumbowyg" tabindex=1>
<h1>mama</h1>
<p>written text</p>
</div>
<script>
$("#pagebody_1").restInPlace();
</script>
</div>
// Define the Trumbowyg form type for RestInPlace
var trumbowyg = {
"trumbowyg" : {
activateForm: function() {
var _this = this;
var domid = this.$element[0].id;
this.$element.trumbowyg({
autogrow: true
});
this.$element[0].focus();
this.$element.keyup(function(e){
if (e.keyCode === 27) {
$("#"+domid).trumbowyg("destroy");
return _this.abort();
}
});
return this.$element.focusout(function() {
var updateRequest, data;
data = _this.getEncodedTokenAuthenticationParams();
data["_method"] = 'put';
data["" + _this.objectName + "[" + _this.attributeName + "]"] = _this.$element[0].innerHTML;
_this.$element.trigger('update.rest-in-place', data);
updateRequest = _this.ajax({
type: "post",
data: data
});
updateRequest.done(function(data) {
if (data) {
return _this.loadSuccessCallback(data);
} else {
return _this.loadViaGET();
}
});
updateRequest.fail(function(jqXHR, textStatus) {
if (jqXHR.status === 200 && textStatus === "parsererror") {
return _this.loadViaGET();
} else {
_this.$element.trigger('failure.rest-in-place');
return _this.abort();
}
});
_this.$element.trumbowyg("destroy");
$(domid).restInPlace();
// return _this.update(); // does not work
});
},
getValue: function() {
return null;
return this.$element[0].innerHTML; // does not do anything
}
}
};
// Extend RestInPlaceEditor with Trumbowyg form type
$.extend(RestInPlaceEditor.forms, trumbowyg);
@ringe
Copy link
Author

ringe commented Mar 1, 2014

I've made a pull request at janv/rest_in_place#47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment