这是我的用户模型 Users.model:
我希望使用一些在模型中“预”计算的属性。我的解决方案是在toJSON()函数中注入属性,但在视图中我必须使用:
有没有办法为用户创建属性或某些方法?比如:
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()函数中注入属性,但在视图中我必须使用:
<%= users.toJSON().link %>
有没有办法为用户创建属性或某些方法?比如:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 3,
maxLength: 30
},
myPersonalAttribute: function(){
return "Value"
}
}
myPersonalAttribute
的示例应该可以正常工作(除了缺少}
)。 你试过了吗? - sgress454