首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得Strongloop回送模型?

如何获得Strongloop回送模型?
EN

Stack Overflow用户
提问于 2014-10-20 09:26:04
回答 2查看 7K关注 0票数 13

这太让人抓狂了,我怎么才能得到一个回送模型,这样我就可以通过编程来使用它了?我有一个名为"Notification“的持久化模型。我可以使用REST资源管理器与其交互。我希望能够在服务器中使用它,即Notification.find(...)。我执行了app.models(),可以看到它被列出了。我已经这样做了:

代码语言:javascript
复制
var Notification = app.models.Notification;

然后得到一个巨大的“未定义的”。我已经这样做了:

代码语言:javascript
复制
var Notification = loopback.Notification;
app.model(Notification);
var Notification = app.models.Notification;

和另一个大胖子“未定义”。

请解释我需要做的所有工作才能获得我定义的模型:

代码语言:javascript
复制
slc loopback:model

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2014-10-24 15:50:48

您可以使用ModelCtor.app.models.OtherModelName从自定义方法访问其他模型。

代码语言:javascript
复制
/** common/models/product.js **/
module.exports = function(Product) {
  Product.createRandomName = function(cb) {
    var Randomizer = Product.app.models.Randomizer;
    Randomizer.createName(cb);
  }

  // this will not work as `Product.app` is not set yet
  var Randomizer = Product.app.models.Randomizer;
}

/** common/models/randomizer.js **/
module.exports = function(Randomizer) {
  Randomizer.createName = function(cb) {
    process.nextTick(function() { 
      cb(null, 'random name');
    });
  };
}

/** server/model-config.js **/
{
  "Product": {
    "dataSource": "db"
  },
  "Randomizer": {
    "dataSource": null
  }
}
票数 10
EN

Stack Overflow用户

发布于 2015-07-12 16:07:34

我知道这篇文章很久以前就在这里了。但由于最近几天我收到了同样的问题,下面是我用最新的loopback api得出的结论:

  • Loopback 2.19.0(7月12日的最新版本)
  • 应用程序接口,获取模型附加到的应用程序对象。示例:

您可以通过如下方式获取您的模型所附加的应用程序:

ModelX.js

代码语言:javascript
复制
module.exports = function(ModelX) {
   //Example of disable the parent 'find' REST api, and creat a remote method called 'findA'
	var isStatic = true;
	ModelX.disableRemoteMethod('find', isStatic);

	ModelX.findA = function (filter, cb) {
      
      //Get the Application object which the model attached to, and we do what ever we want
	ModelX.getApp(function(err, app){
	  if(err) throw err;
	  //App object returned in the callback
	  app.models.OtherModel.OtherMethod({}, function(){
	  if(err) throw err;
	  //Do whatever you what with the OtherModel.OtherMethod
      //This give you the ability to access OtherModel within ModelX.
      //...
      });
     });
	}
    
   //Expose the remote method with settings.
	ModelX.remoteMethod(
	 'findA',
	 {
		description: ["Remote method instaed of parent method from the PersistedModel",
			"Can help you to impliment your own business logic"],
		http:{path: '/finda', verb: 'get'},
		  accepts: {arg:'filter', 
		  type:'object', 
		  description: 'Filter defining fields, where, include, order, offset, and limit',
		http:{source:'query'}},
			returns: {type:'array', root:true}
		}
	);
};

看起来我在这里的代码块格式做得不太好…

此外,你应该注意这个'getApp‘被调用的时间,这很重要,因为如果你在初始化模型的时候很早就调用了这个方法,就会发生类似'undefined’的错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26457327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档