[英]How to create 'calculated' fields at Waterline/Sails.js Model?
這是我的Users.model:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 3,
maxLength: 30
},
username: {
type: 'string',
required: true,
},
toJSON: function() {
var obj = this.toObject();
obj.link = sails.config.globals.baseUrl + sails.config.routes.user + obj.id;
return obj;
}
}
};
我想要的是使用一些在模型中“預先”計算的屬性。 我的解決方案是在toJSON()函數中注入attr,但在我必須使用的視圖中:
<%= users.toJSON().link %>
有一種方法可以為用戶創建屬性或某些方法嗎? 喜歡:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 3,
maxLength: 30
},
myPersonalAttribute: function(){
return "Value"
}
}
您可以使用屬性方法返回派生值。 請在此處查看我對您的github問題的回復: https : //github.com/balderdashy/waterline/issues/626#issuecomment-54192398
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 3,
maxLength: 30
},
myPersonalAttribute: function() {
return "Value"
},
toJSON: function() {
var obj = this.toObject()
obj.myPersonalAttribute = this.myPersonalAttribute()
return obj
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.