2015-12-17 57 views
0

我正在研究環回,並想知道是否可以從一個請求保存到多個模型。說..一個帖子有許多圖像的標籤。用戶形式將具有以下:在環回中保存多個模型

  • 職銜
  • 發佈說明
  • 標籤名稱(A多字段例如:[ '科幻', '科幻', '暢銷商品']
  • 圖像文件(希望能處理上傳到AWS,可能與船長-S3文件?)

我怎麼能夠堅持多模型這樣嗎?這是不是你用鉤子嗎?

回答

2

您可以創建一個模型RemoteMethods,可以定義參數,所以在你的例子,你可以在你的後創建模型是這樣的:

// 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'} 
    } 
); 
+1

你是最好的!我只是注意到了這一點,以及我在玩弄它,但這對我來說非常清晰。謝謝! – Handonam