Frage

I'm new with geddy and I'm getting confuse on how to use the model events.

My model has a slug field and I want to generate the slug (base on the name they entered) before I save any records.

In other words how do I do this in geddy?

rails model:

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

sample model code: 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'}); and p.save(function(e,d){ console.log(d); }) slug is undefined

War es hilfreich?

Lösung

You can use the beforeValidate lifecycle method to do this.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top