core/server/models/base/index.js
function initialize() {
        var self = this;

        // NOTE: triggered before `creating`/`updating`
        this.on('saving', function onSaving(newObj, attrs, options) {
            if (options.method === 'insert') {
                // id = 0 is still a valid value for external usage
                if (_.isUndefined(newObj.id) || _.isNull(newObj.id)) {
                    newObj.setId();
                }
            }
        });

        [
            'fetching',
            'fetching:collection',
            'fetched',
            'fetched:collection',
            'creating',
            'created',
            'updating',
            'updated',
            'destroying',
            'destroyed',
            'saving',
            'saved'
        ].forEach(function (eventName) {
            var functionName = 'on' + eventName[0].toUpperCase() + eventName.slice(1);

            if (functionName.indexOf(':') !== -1) {
                functionName = functionName.slice(0, functionName.indexOf(':'))
                    + functionName[functionName.indexOf(':') + 1].toUpperCase()
                    + functionName.slice(functionName.indexOf(':') + 2);
                functionName = functionName.replace(':', '');
            }

            if (!self[functionName]) {
                return;
            }

            self.on(eventName, self[functionName]);
        });

        // @NOTE: Please keep here. If we don't initialize the parent, bookshelf-relations won't work.
        proto.initialize.call(this);
    }
function initialize() {
        var self = this;

        // NOTE: triggered before `creating`/`updating`
        this.on('saving', function onSaving(newObj, attrs, options) {
            if (options.method === 'insert') {
                // id = 0 is still a valid value for external usage
                if (_.isUndefined(newObj.id) || _.isNull(newObj.id)) {
                    newObj.setId();
                }
            }
        });

        [
            'fetching',
            'fetching:collection',
            'fetched',
            'fetched:collection',
            'creating',
            'created',
            'updating',
            'updated',
            'destroying',
            'destroyed',
            'saving',
            'saved'
        ].forEach(function (eventName) {
            var functionName = 'on' + eventName[0].toUpperCase() + eventName.slice(1);

            if (functionName.indexOf(':') !== -1) {
                functionName = functionName.slice(0, functionName.indexOf(':'))
                    + functionName[functionName.indexOf(':') + 1].toUpperCase()
                    + functionName.slice(functionName.indexOf(':') + 2);
                functionName = functionName.replace(':', '');
            }

            if (!self[functionName]) {
                return;
            }

            self.on(eventName, self[functionName]);
        });

        // @NOTE: Please keep here. If we don't initialize the parent, bookshelf-relations won't work.
        proto.initialize.call(this);
    }
function initialize ( ) { var self = this ; // NOTE: triggered before `creating`/`updating` this . on ( 'saving' , function onSaving ( newObj , attrs , options ) { if ( options . method === 'insert' ) { // id = 0 is still a valid value for external usage if ( _ . isUndefined ( newObj . id ) || _ . isNull ( newObj . id ) ) { newObj . setId ( ) ; } } } ) ; [ 'fetching' , 'fetching:collection' , 'fetched' , 'fetched:collection' , 'creating' , 'created' , 'updating' , 'updated' , 'destroying' , 'destroyed' , 'saving' , 'saved' ] . forEach ( function ( eventName ) { var functionName = 'on' + eventName [ 0 ] . toUpperCase ( ) + eventName . slice ( 1 ) ; if ( functionName . indexOf ( ':' ) !== - 1 ) { functionName = functionName . slice ( 0 , functionName . indexOf ( ':' ) ) + functionName [ functionName . indexOf ( ':' ) + 1 ] . toUpperCase ( ) + functionName . slice ( functionName . indexOf ( ':' ) + 2 ) ; functionName = functionName . replace ( ':' , '' ) ; } if ( ! self [ functionName ] ) { return ; } self . on ( eventName , self [ functionName ] ) ; } ) ; // @NOTE: Please keep here. If we don't initialize the parent, bookshelf-relations won't work. proto . initialize . call ( this ) ; }
Bookshelf `initialize` - declare a constructor-like method for model creation
Bookshelf initialize - declare a constructor - like method for model creation