在环回中保存多个模型 [英] Save multiple models in loopback
本文介绍了在环回中保存多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究回送,想知道是否可以从一个请求中保存到多个模型.说..一个帖子有很多带有很多图像的标签.用户表单将具有以下内容:
I'm doing research on loopback and was wondering if it's possible to save to multiple models from one request. Say.. an Post has many Tags with many Images. A user form would have the following:
- 帖子标题
- 帖子说明
- 标签名称(一个多字段.例如:['Sci-Fi','Fiction','BestSeller']
- 图像文件(希望处理上传到AWS的文件,也许使用skipper-s3吗?)
我如何能够在这样的多个模型上坚持下去?这是您用钩子做的事吗?
How would I be able to persist on multiple models like this? Is this something you do with a hook?
推荐答案
您可以创建 RemoteMethods
在模型中,它可以定义参数,因此在您的示例中,您可以在Post模型中创建以下内容:
You can create RemoteMethods
in a Model, which can define parameters, so in your example you could create something like this in your Post model:
// 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'}
}
);
这篇关于在环回中保存多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文