Javascript 如何创建';计算';水线处的田地/Sails.js模型?

Javascript 如何创建';计算';水线处的田地/Sails.js模型?,javascript,sails.js,waterline,sails-mongo,Javascript,Sails.js,Waterline,Sails Mongo,这是我的用户。型号: module.exports = { attributes: { name: { type: 'string', required: true, minLength: 3, maxLength: 30 }, username: { type: 'string', requ

这是我的用户。型号:

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 %> 

可以使用属性方法返回派生值。请参见我对github问题的回答:

您最后一个使用
myPersonalAttribute
的示例应该可以正常工作(忽略
}
)。你真的试过了吗?我试过了,我把}放进去了,但我没用。说myPers。。。方法不存在。我同意这是正确的,但这不是OP说他们已经在做的,并且由于某种原因不起作用吗?
module.exports = {

       attributes: {

        name: {
            type: 'string',
            required: true,
            minLength: 3,
            maxLength: 30
        },
        myPersonalAttribute: function(){
           return "Value"   
        }
}
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
        }
    }
}