2016-12-20 9 views
0

私はSailsJsに新たなんだと私はデフォルトのメソッドを介してオーバーライドを実行するには、例えば、足場を生成:SailsJs - 上書きコントローラのデフォルトメソッド

を作成しますので、アイデアがにありますcreateへの呼び出しの前にsomeStuff()を実行します。基本的に、スキャフォールディングの後に出てくるデフォルトの作成の前にいくつかの機能を追加していますが、コードを書き直すことを避けるためにその機能を呼びたいと思います。

私が実行するコマンドは次のとおりです。ここで

sails generate api user 

は私のコントローラのコードである:ここで

/** 
* UserController 
* 
* @description :: Server-side logic for managing users 
* @help  :: See http://sailsjs.org/#!/documentation/concepts/Controllers 
*/ 

module.exports = { 
    create: function (req, res) { 
    someStuff(); 
    call_to_default_create()<--- What to know if posible? 
    },   
}; 

は、モデルのコードです:

/** 
* User.js 
* 
* @description :: TODO: You might write a short summary of how this model works and what it represents here. 
* @docs  :: http://sailsjs.org/documentation/concepts/models-and-orm/models 
*/ 

module.exports = { 
    attributes: { 
    name: { 
     type: 'string', 
     required: true, 
     minLength: 2 
    }, 
    last_name: { 
     type: 'string', 
     required: true, 
     minLength: 2 
    }, 
    email: { 
     type: 'string', 
     email: true, 
     required: true, 
     unique: true 
    }, 
    encrypted_password: { 
     type: 'string' 
    } 
    } 
} 

私は帆を動作する方法は、継承ではないと思われるが、私は何とかこれをエミュレートすることができます?

答えて

1

あなたはsails.hooks.blueprints.middleware.create(req, res);を探しています。だからあなたのカスタムcreate方法は次のようになります。

/** 
* UserController 
* 
* @description :: Server-side logic for managing users 
* @help  :: See http://sailsjs.org/#!/documentation/concepts/Controllers 
*/ 

module.exports = { 
    create: function (req, res) { 
    someStuff(); 
    return sails.hooks.blueprints.middleware.create(req, res); 
    },   
}; 

私も、このようにオーバーライド青写真は、実際にどこかに文書を見つけるので、ご自身の責任で使用することはできません。 =)

+0

ありがとう、私はこれを適用し、それが動作します。 –

関連する問題