Reputation: 31
I am using subclassing to add custom properties to standard fabric objects like so:
var IdRect: any = fabric.util.createClass(fabric.Rect, {
type: 'idRect',
initialize: function(id, dbType, options) {
this.callSuper('initialize', options);
this.id = id;
this.dbType = dbType;
},
toObject: function () {
return fabric.util.object.extend(this.callSuper('toObject'), {
});
},
_render: function (ctx) {
this.callSuper('_render', ctx);
}
});
IdRect.fromObject = function(options, callback) {
var idRect = new IdRect(null, null, options);
callback && callback(idRect);
return idRect;
}
One problem that I run into when trying to clone a selection of those custom objects is that fabric.util.getKlass returns null (Cannot read property 'fromObject' of undefined). Cloning them individually works fine.
Upvotes: 1
Views: 366
Reputation: 31
Turns out you need to assign the property to the fabric library like so
fabric.IdRect = fabric.util.createClass(...)
or for typescript first assign fabric to a variable of type any to bypass the type system:
var Fabric: any = fabric;
Fabric.IdRect = fabric.util.createClass(...)
Upvotes: 1