如何使用sails.js为模型定义实例方法

Adr*_*ien 11 model function node.js sails.js waterline

如何在Sails中为对象定义函数/实例方法?

在Waterline doc(https://github.com/balderdashy/waterline)中,他们说:

var User = Waterline.Collection.extend({
...
  attributes: {
    ...
    // You can also define instance methods here
    fullName: function() {
      return this.firstName + ' ' + this.lastName
    }
  },
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在Sails中的模型中的属性中定义实例方法时,该函数不会添加到对象中.难道我做错了什么 ?

环境:风帆(v0.8.94),节点(v0.8.16)

Adr*_*ien 14

您可以在具有sails 0.9.0的模型中定义实例方法,如下所示:

module.exports = {
  attributes: {     
    name: {
      type: 'STRING',
      defaultsTo: 'zooname'
    },
    instanceMethod: function(){
      // your code
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

用法示例:

ClientHit.findOne({}).exec(function(err, model){
  model.instanceMethod(); //use your instance method
});
Run Code Online (Sandbox Code Playgroud)