Last active
October 10, 2018 19:26
-
-
Save JelteMX/67b132541586e29f757be3bb20518031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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