简体   繁体   English

我如何使用Geddy模型事件

[英]How do I use Geddy Model Events

I'm new with geddy and I'm getting confuse on how to use the model events. 我是geddy的新手,我对如何使用模型事件感到困惑。

My model has a slug field and I want to generate the slug (base on the name they entered) before I save any records. 我的模型有一个slug字段,我想在保存任何记录之前生成一个slug(基于它们输入的名称)。

In other words how do I do this in geddy? 换句话说,如何在geddy中做到这一点?

rails model: 滑轨型号:

before_save :generateSlug
private:
 def generateSlug
   self.slug = self.name.parameterize
 end

sample model code: model/page.js 示例模型代码:model / page.js

slugify = require('slug');
var Page = function(){
  this.defineProperties({
   slug: {type: 'string'},
   name: {type: 'string', required: true}
  });

  this.beforeSave = function(){
   this.slug = slugify(this.name);
  }
}
exports.Page = Page;

When I run p = geddy.model.Page.create({name: 'hello world'}); 当我运行p = geddy.model.Page.create({name: 'hello world'}); and p.save(function(e,d){ console.log(d); }) slug is undefined p.save(function(e,d){ console.log(d); })未定义

You can use the beforeValidate lifecycle method to do this. 您可以使用beforeValidate生命周期方法来执行此操作。

Try this: 尝试这个:

this.beforeValidate = function () {
  this.slug = slugify(this.name);
};

Note: This did not work prior to Model@0.3.2 , which had a bugfix for inconsistent lifecycle methods. 注意:在Model@0.3.2之前,此方法Model@0.3.2 ,该模型有针对生命周期方法不一致的错误修正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM