简体   繁体   中英

Is there a way to define a virtual field in Geddy model?

Is it possible to define a temp field / virtual field in geddy model?

Like in the form I've use the input fields tmpFirstName and tmpLastName but when submitted I want to store the information in a single column name .

Thanks

This can be trivially achieved with the new lifecycle methods (thanks to you!).

In your controller:

  this.create = function (req, resp, params) {
    var self = this
      , person = geddy.model.Person.create(params);

    person.firstname = params.firstname;
    person.lastname = params.lastname;

    if (!person.isValid()) {
      this.respondWith(person);
    }
    else {
      person.save(function(err, data) {
        if (err) {
          throw err;
        }
        self.respondWith(person, {status: err});
      });
    }
  };

In your model:

  this.defineProperties({
    name: {type: 'string'}
  });

  this.beforeSave = function () {
    this.name = this.firstname + ' ' + this.lastname;
  }

Note that you don't declare the "virtual" properties, otherwise geddy will store them in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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