如何用sails.js定义模型的实例方法 [英] How to define instance methods for models with sails.js

查看:111
本文介绍了如何用sails.js定义模型的实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

  var User = Waterline.Collection.extend({
...
attributes:{
...
//你也可以在这里定义实例方法
fullName :function(){
return this.firstName +''+ this.lastName
}
},
}

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



环境:
Sails(v0.8.94),Node(v0.8.16)



 

module.exports = {
attributes:{
name:{
键入:'STRING',
defaultsTo:'zooname'
},
instanceMethod:function(){
//您的代码
}
}
};

使用范例:

  ClientHit.findOne({})。exec(function(err,model){
model.instanceMethod(); //使用您的实例方法
});


How can I define functions/instance method for objects in Sails ?

In Waterline doc (https://github.com/balderdashy/waterline) they say:

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

But when I try do define an instance method in attributes in a model in Sails, the function is not added to the object. Am I doing something wrong ?

Environment: Sails (v0.8.94), Node (v0.8.16)

解决方案

You can define instance methods in models with sails 0.9.0 like this:

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

Usage example:

ClientHit.findOne({}).exec(function(err, model){
  model.instanceMethod(); //use your instance method
});

这篇关于如何用sails.js定义模型的实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆