Javascript 在环回中保存多个模型
我正在研究环回,想知道是否可以从一个请求保存到多个模型。说。。一篇文章有许多带有许多图像的标签。用户表单将具有以下内容:Javascript 在环回中保存多个模型,javascript,loopbackjs,strongloop,Javascript,Loopbackjs,Strongloop,我正在研究环回,想知道是否可以从一个请求保存到多个模型。说。。一篇文章有许多带有许多图像的标签。用户表单将具有以下内容: 职称 职位描述 标记名称(多字段,例如:[“科幻”、“小说”、“畅销书”] 图像文件(希望处理上传到AWS的文件,可能使用skipper-s3?) 我如何能够在这样的多个模型上持久化?这是您使用钩子所做的吗?您可以在一个模型中创建,该模型可以定义参数,因此在您的示例中,您可以在Post模型中创建类似的内容: // remote method, defined below
- 职称
- 职位描述
- 标记名称(多字段,例如:[“科幻”、“小说”、“畅销书”]
- 图像文件(希望处理上传到AWS的文件,可能使用skipper-s3?)
// 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模型中创建如下内容:
// 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'}
}
);
你是最棒的!我只是注意到了这一点,我也在玩弄它,但这对我来说非常清楚。谢谢!你是最棒的!我只是注意到了这一点,我也在玩弄它,但这对我来说非常清楚。谢谢!