Sails.js equivalent of Rails before_save?

Sails.js equivalent of Rails before_save?

本文关键字:save before Rails js equivalent of Sails      更新时间:2023-09-26

我以前做过一些rails回调(http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)

我想在保存模型(图像)之前调用一个函数,该函数将在将图像保存到数据库之前获得该图像的宽度/高度。使用此功能:

function getMeta(url){
  var img = new Image();
  img.onload = function(){
    s = {w :this.width, h:this.height;}
  };
  img.src = url;
  return s;
}
module.exports = {
  attributes: {
    source: {
      type: 'string',
      required: true
    },
    height: {
      type: 'string',
      defaultsTo: getMeta(this.source).h
    },
    width: {
      type: 'string',
      defaultsTo: getMeta(this.source).w
    }
  }
};

缺点是,1)它不起作用;2) 我调用该函数两次。

有没有办法在创建之前调用该函数,设置2个局部变量,然后用这些值保存它?

Sails.js具有生命周期回调,包括beforeUpdatebeforeCreate