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.