Skip to content

Instantly share code, notes, and snippets.

@JelteMX
Last active October 10, 2018 19:26
Show Gist options
  • Save JelteMX/67b132541586e29f757be3bb20518031 to your computer and use it in GitHub Desktop.
Save JelteMX/67b132541586e29f757be3bb20518031 to your computer and use it in GitHub Desktop.
import {
defineWidget,
log,
runCallback,
fetchAttr
} from 'widget-base-helpers';
export default defineWidget('Widget', false, {
attribute: '', // this key is set in your widget XML
_obj: null,
constructor() {
this.log = log.bind(this);
this.runCallback = runCallback.bind(this);
this.fetch = fetchAttr.bind(this);
},
postCreate() {
this.log('postCreate', this._WIDGET_VERSION);
},
update(obj, callback) {
this._obj = obj;
this._updateRendering(callback);
},
async _updateRendering(callback) {
// A bit more complicated example, where you fetch an attribute (over reference for example).
try {
// Using async/await is a safe way where you can wait for the fetch.
const data = await this.fetch(this._obj, this.attribute);
} catch (e) {
// Do something with the error;
}
this.runCallback(callback);
}
});
import {
defineWidget,
log,
runCallback,
} from 'widget-base-helpers';
export default defineWidget('Widget', false, {
attribute: '', // this key is set in your widget XML
_obj: null,
constructor() {
this.log = log.bind(this);
this.runCallback = runCallback.bind(this);
},
postCreate() {
this.log('postCreate', this._WIDGET_VERSION);
},
update(obj, callback) {
this._obj = obj;
if (this._obj) {
const value = this._obj.get(this.attribute); // Now you get the value from attribute of the context object;
}
this.runCallback(callback);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment