在环回中保存多个模型
Save multiple models in loopback
我正在研究环回,想知道是否可以从一个请求保存到多个模型。说.. Post 有很多标签和很多图片。用户表单将具有以下内容:
- Post 标题
- Post 说明
- 标签名称(多字段。例如:['Sci-Fi'、'Fiction'、'BestSeller']
- 图像文件(希望处理上传到AWS的文件,也许用skipper-s3?)
我怎样才能坚持使用这样的多个模型?这是你用钩子做的事吗?
您可以在可以定义参数的模型中创建 RemoteMethods
,因此在您的示例中,您可以在 Post 模型中创建如下内容:
// remote method, defined below
Post.SaveFull = function(title,
description,
tags,
imageUrl,
cb) {
var models = Post.app.Models; // provides access to your other models, like 'Tags'
Post.create({"title": title, "description": description}, function(createdPost) {
foreach(tag in tags) {
// do something with models.Tags
}
// do something with the image
// callback at the end
cb(null, {}); // whatever you want to return
})
}
Post.remoteMethod(
'SaveFull',
{
accepts: [
{arg: 'title', type: 'string'},
{arg: 'description', type: 'string'},
{arg: 'tags', type: 'object'},
{arg: 'imageUrl', type: 'string'}
],
returns: {arg: 'Post', type: 'object'}
}
);
我正在研究环回,想知道是否可以从一个请求保存到多个模型。说.. Post 有很多标签和很多图片。用户表单将具有以下内容:
- Post 标题
- Post 说明
- 标签名称(多字段。例如:['Sci-Fi'、'Fiction'、'BestSeller']
- 图像文件(希望处理上传到AWS的文件,也许用skipper-s3?)
我怎样才能坚持使用这样的多个模型?这是你用钩子做的事吗?
您可以在可以定义参数的模型中创建 RemoteMethods
,因此在您的示例中,您可以在 Post 模型中创建如下内容:
// remote method, defined below
Post.SaveFull = function(title,
description,
tags,
imageUrl,
cb) {
var models = Post.app.Models; // provides access to your other models, like 'Tags'
Post.create({"title": title, "description": description}, function(createdPost) {
foreach(tag in tags) {
// do something with models.Tags
}
// do something with the image
// callback at the end
cb(null, {}); // whatever you want to return
})
}
Post.remoteMethod(
'SaveFull',
{
accepts: [
{arg: 'title', type: 'string'},
{arg: 'description', type: 'string'},
{arg: 'tags', type: 'object'},
{arg: 'imageUrl', type: 'string'}
],
returns: {arg: 'Post', type: 'object'}
}
);