内容简介:因为get方法相当于是直接挂在类prototype上的取值函数,是在super()方法执行时就已经存在的了代码1是在官方文档示例中,另一种对model.relation的定义,试图在继承了think.Model的用户自定义model类的构造函数中通过this给relation赋值,从而传递到 Relation的实例中,这是做法无法实现的。OS Platform:
module.exports = class extends think.Model {
// 配置关联关系
get relation() {
return {
cate: { // 配置跟分类的关联关系
type: think.Model.MANY_TO_MANY,
...
},
comment: { // 配置跟评论的关联关系
}
}
}
}
复制代码
因为get方法相当于是直接挂在类prototype上的取值函数,是在super()方法执行时就已经存在的了
这种构造函数里的赋值方法是有问题
// 代码1
module.exports = class extends think.Model {
constructor(...args){
super(...args);
this.relation = {
comment: think.Model.HAS_MANY,
cate: think.Model.MANY_TO_MANY
};
}
getList2(){
return this.setRelation(true).select();
}
}
复制代码
代码1是在官方文档示例中,另一种对model.relation的定义,试图在继承了think.Model的用户自定义model类的构造函数中通过this给relation赋值,从而传递到 Relation的实例中,这是做法无法实现的。
ENV
OS Platform:
win10 64 复制代码
Node.js Version:
v10.13.0 复制代码
ThinkJS Version:
thinkjs@3.2.10 think-model@1.3.1 复制代码
原因分析
翻看 think-model 中 model.js 和 relation.js 的代码可以发现:
// model.js 代码2
constructor(modelName = '', config = {}) {
if (helper.isObject(modelName)) {
[modelName, config] = ['', modelName];
}
assert(helper.isFunction(config.handle), 'config.handle must be a function');
this.config = config;
this.modelName = modelName;
this.options = {};
this[RELATION] = new Relation(this);
}
复制代码
==在Model类( 代码2
)中,属性 this[Symbol('think-model-relation')]
的实例化是在 代码1
中子类this.relation初始化之前完成的(毕竟要先实行super()),所以 this[RELATION] = new Relation(this)
时,传入的this中,并没有自定义的relation属性。==
// relation.js 代码3
constructor(model) {
this.model = model;
this.relation = this.model.relation || {};
this.relationName = true;
}
复制代码
==继续翻看relation.js( 代码3 )的代码发现,如果Relation类的构造函数没有接收到有效的relation值,则会把this.relation置为一个空对象,而且由于Relation实例化是复制给了一个本地Symbol类型变量,后期在外部或者子类中也无法引用到了,所以初始化之后是无法改变的。==
另一种可行的写法 但是没什么卵用 不如用get
// 代码4
class user extends think.Model {
constructor(...args){
super(...args);
}
/* get relation() {
return {
auths: {
type: think.Model.HAS_MANY,
model: 'user_auths',
fKey: 'id_user',
relation: false
}
}
}*/
async getUserByAuthTypeAndIdentifier(authType, identifier) {
const auth_sql = await this.model('user_auths').setRelation(true).where({auth_type: authType, identifier: identifier}).buildSelectSql();
return this.alias('u').join({
table: auth_sql,
join: 'inner',
as: 'a',
on: {
'id': 'id_user',
}
}).find();
}
}
user.prototype.relation = {
auths: {
type: think.Model.HAS_MANY,
model: 'user_auths',
fKey: 'id_user',
relation: false
}
}
module.exports = user
复制代码
如果想不用get方法实现 我只想到了上述 代码4 这种写法,也就是在原型上扩展一下。
以上所述就是小编给大家介绍的《thinkjs文档中model.relation写法的探讨》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
iOS游戏编程之从零开始
李华明 / 2013-2 / 59.00元
《iOS游戏编程之从零开始:Cocos2d-x与cocos2d引擎游戏开发》是作者继《android游戏编程之从零开始》热销之后编写的又一本、基于cocos2d—x2.x和cocos2d—iphone版本,讲述ios平台游戏开发的新作。《iOS游戏编程之从零开始:Cocos2d-x与cocos2d引擎游戏开发》分为两个部分共11章,内容主要包括cocos2d—x引擎游戏开发的基础,常用的类、方法及......一起来看看 《iOS游戏编程之从零开始》 这本书的介绍吧!