2017-08-09 26 views
0

我有通知模型看起來像這樣迴環:如何添加模型的afterRemote到另一種模式

"use strict"; 

module.exports = function(Notification) { 


}; 

我有另一種模式是郵政:

"use strict"; 

module.exports = function(Post) { 
    Post.prototype.postLike = function(options, cb) { 
      this.likes.add(options.accessToken.userId); 

      cb(null, "sucess"); 
    }; 

    Post.remoteMethod("postLike", { 
    isStatic: false, 
    accepts: [{ arg: "options", type: "object", http: "optionsFromRequest" }], 
    returns: { arg: "name", type: "string" }, 
    http: { path: "/like", verb: "post" } 
    }); 
} 

我要的是在通知模型裏面添加PostRemote方法?

是否有可能在回送?

它不應該是這樣的:

"use strict"; 

module.exports = function(Notification) { 

    var app = require("../../server/server.js"); 
    var post = app.models.Post; 

    post.afterRemote('prototype.postLike', function(context, like, next) { 
    console.log('Notification after save for Like comment'); 
    }); 
}; 

但是,這是行不通的。

注:我能做到這一點Post模型本身,但我想補充我所有的通知邏輯的通知模型簡化和未來的定製。

回答

2

您可以使用活動來完成。

迴環可發射started事件,當它啓動的所有啓動腳本加載here

Notification模型後這樣做:

"use strict"; 

    module.exports = function(Notification) { 

     var app = require("../../server/server.js"); 

     app.on('started', function(){ 
     var post = app.models.Post; 
     post.afterRemote('prototype.postLike', function(context, like, next) { 
     console.log('Notification after save for Like comment'); 
     }); 
    }); 
    }; 

或者創建啓動腳本,併發出像「allModelsLoaded自定義事件」。所以請確保引導腳本是最後一個要運行的腳本。引導腳本默認按字母順序運行。因此,製作z.js並在那裏發出該自定義事件,然後在Notification模型中聽該事件。

1

環回啓動過程首先加載模型,然後在所有模型加載後調用啓動腳本。如果您的目標是跨模型合併事務,則最好在引導腳本中執行此操作,而不是在model.js文件中執行此操作。

+0

我會檢查一下。有沒有什麼辦法可以在沒有啓動腳本的情況下在模型中執行它?或者像我應該在我的所有模型加載後檢查它。 –

相關問題