2015-07-21 58 views
4

我繼承了一個模型,在環回像這 -遠程方法沒有出現在環回探險

{ 
    "name": "MyModel", 
    "base": "AnotherModel", 
    "idInjection": false, 
    "properties": { 
     "value": { 
     "type": "String" 
     }, 
     "display": { 
     "type": "String" 
     } 
    }, 
    "validations": [], 
    "relations": {}, 
    "acls": [], 
    "methods": [] 
} 

雖然我能夠調用​​所有的遠程方法從我MyModel.js文件。但是,我的資源管理器中沒有顯示​​的遠程方法。如何讓我的繼承模型的所有遠程方法顯示在資源管理器中?

回答

3

出現這種情況的原因是,當你叫AnotherModel.remoteMethod,它只註冊了該模型,而不是模型是基於這種遠程方法。要調用它是基於AnotherModel的,你可以做到這一切型號:

var originalSetup = AnotherModel.setup; 
AnotherModel.setup = function() { // this will be called everytime a 
    // model is extended from this model. 

    originalSetup.apply(this, arguments); // This is necessary if your 
    // AnotherModel is based of another model, like PersistedModel. 

    this.remoteMethod('yourMethod', { 
    .. 
    }); 
}; 

我從here瞭解到這一點,也檢查persistedModel如何擁有它的所有在基於它的所有型號的遠程方法。

還要確保基本模型是公共due to this issue

0

您應該使用setup()來 「揭露」 AnotherModels'功能。 http://loopback.io/doc/en/lb2/Extending-built-in-models.html#setting-up-a-custom-model 然後在設置部分添加您的遠程方法如下所述:http://loopback.io/doc/en/lb2/Remote-methods#example

這裏是例子:(它也可以適用於靜態函數)

// AnotherModel.js 
module.exports = function (AnotherModel) { 
    ... 
    // suppose you have two functions to inherit 'function1' and 'function2' 
    AnotherModel.prototype.function1 = function() {...} 
    AnotherModel.function2 = function() {...} 
    // The loopback.Model.extend() function calls setup() 
    // so code you put in setup() will automatically get 
    // executed when the model is created. 
    var originalSetup = AnotherMoldel.setup; 
    AnotherMoldel.setup = function() { 
     // call the original setup! 
     originalSetup.apply(this, arguments); 
     // 'this' will points to MyModel during MyModel setup 
     // first param should match with function name 
     this.remoteMethod('function1', { 
      accepts: {...}, 
      returns: {...}, 
      http: {...}}); 
     this.remoteMethod('function2', { 
      accepts: {...}, 
      returns: {...}, 
      http: {...}}); 
    } 
} 

將在API瀏覽器中顯示了。

我希望它能幫助,請讓我知道,如果你發現了另一個解決方案! (因爲這個帖子已經超過一年了)

乾杯