vue-mc
vue-mc copied to clipboard
defineProperty with configurable
Im trying overwrite getters&setters for simple relations support, but get error can't redefine non-configurable property
. Can you add please configurable: true, in registerAttribute for Object.defineProperty?
I'm curious to see what you're trying to implement here. Would you mind sharing your code and concept? Might be a good case to support natively here.
Its very simply, but work for me ...
BaseModel
constructor(attributes = {}, collection = null, options = {}) {
super(attributes, collection, options);
Vue.set(this, '_relations', {}); // Relations
this.assignRelations();
}
get relations() {
return this._relations;
}
definedRelations(){return {}}
setRelation(name, config, relation){
if (relation && _.isPlainObject(relation)) {
relation = new config.class(relation);
}
var foreignKey = config.foreignKey || `${name}_id`;
var localKey = config.localKey || 'id';
Vue.set(this._relations, name, relation);
var value = (relation)?relation[localKey]:null;
this.set(foreignKey,value);
return this;
}
getRelation(name){
return this._relations[name];
}
registerRelation(name,config){
var _that = this;
var names = _.unionBy([name],config.aliases);
_.each(names,function (item) {
var exist = !_.isUndefined(_that[item]); // I can't find how to set Relations before super() method.
Object.defineProperty(_that, item, {
get: () => _that.getRelation(name),
set: (relation) => _that.setRelation(name, config, relation),
});
if (exist){
_that[item] = _.cloneDeep(_that.get(item));
_that.unset(item);
}
});
return this;
}
assignRelations(){
let that = this;
_.each(this.definedRelations(),function (config, name) {
that.registerRelation(name,config);
});
}
Model
definedRelations(){
return {
contractor:{class:ContractorModel},
category:{class:CategoryModel},
town_from:{class:GeographyModel,foreignKey:'from_town_id',aliases:['townFrom']},
b_point_profile:{class:BPointsProfilesModel,foreignKey:"bpoint_profile_id",aliases:['BPointProfile']},
avatar:{class:AvatarModel},
s_c_offer:{class:SCOfferModel,foreignKey:"scoffer_id",aliases:['SCOffer']},
}
}
Usage
var offerModel = new OfferModel();
var contractorModel = new ContractorModel({id:123,...});
offerModel.contractor = ContractorModel;
console.log(offer.contractor_id); // 123
Can't see a reason why configurable can't be true
. 👍