Recommended way to extend mvc.View #3440
IntroductionHello, while reading the tutorial and looking at all the examples I found so far I see use of the custom class PortHandle extends mvc.View<undefined, SVGElement> {
tagName = "circle"instead of const PortHandle = mvc.View.extend({
tagName: "circle",the property is not yet set by the time the code that reads it ( I tried various other syntaxes like class PortHandle extends mvc.View<undefined, SVGElement> {
static tagName = "circle"or class PortHandle extends mvc.View<undefined, SVGElement> {
constructor(){
this.tagName = "circle"
}but with no success. The only way I managed a class like this to work is class PortHandle extends mvc.View<undefined, SVGElement> {
// ...
}
Object.assign(PortHandle.prototype, {
tagName: "circle",
})Which ensures the property is present in the prototype chain before any instance is created. I can't help but think this is not the intended way to do things, so what am I missing please? Thanks Steps to reproduceNo response Restrictions & ConstraintsNo response Does your question relate to JointJS or JointJS+. Select both if applicable.JointJS, JointJS+ |
Replies: 1 comment 1 reply
|
Hi, use class PortHandle extends mvc.View<undefined, SVGElement> {
preinitialize() {
this.tagName = 'circle';
this.svgElement = true;
}
// the rest as regular class members
}Same applies to any property read while the element is being created:
Your |
Hi, use
preinitialize()— that's the hook for exactly this case:Same applies to any property read while the element is being created:
className,attributes,style,id,events, andsvgElement(without the latter yourcircleends up in the XHTML namespace instead of SVG, andthis.velis never set up). Everything read later (children,DETACHABLE,UPDATE_PRIORITY, your own state and methods) is fine as a normal class field.mvc.Viewinherits Backbone's constructor contract, andpreinitializewas add…