Inherit attributes and lifecycle functions of Sails.js models

后端 未结 1 1597
半阙折子戏
半阙折子戏 2020-12-18 01:47

I want to create a custom set of attributes and lifecycle methods that are shared between all my Sails.js models.

Sails.js automatically creates and registers the m

相关标签:
1条回答
  • 2020-12-18 02:15

    Using config/models.js to provide global defaults for models is perfectly valid. Concerning overriding instance and class methods there is nothing fancy to watch out for according to my tests. Defining a property / method present in sails.config.models in a model definition will override it for this model, leaving it undefined won't.

    Definitions:

    // config/models.js
    module.exports.models = {
      attributes: {
        // base model instanceMethod
        toJSON: function() {
          console.log('base.toJSON');
          return this.toObject();
        }
      },
    
      // base model classMethod
      test: function() {
        console.log('base.test');
      }
    };
    
    
    // api/models/first.js
    module.exports = {
      attributes: {
    
      },
    
      // Overriding classMethods and lifecycle callbacks
      test: function() {
        console.log('first.test');
      }
    };
    
    // api/models/second.js
    module.exports = {
      attributes: {
        // Overriding instance methods and attributes
        toJSON: function() {
          console.log('second.toJSON');
          return this.toObject();
        }
      },
    }
    

    Tests

    > sails.models.first.test();
    >'first.test' // sails.config.models.test overridden
    
    > sails.models.first.findOne(1).exec(err,res){ res.toJSON();  });
    > 'base.toJSON' // sails.config.models.attributes.toJSON not overridden
    
    > sails.models.second.test();
    > 'base.test'; // sails.config.models.test not overridden
    
    > sails.models.second.findOne(1).exec(err,res) { res.toJSON(); });
    > 'second.toJSON' // sails.config.models.attributes.toJSON overridden
    
    0 讨论(0)
提交回复
热议问题