使用脚本更新关系会覆盖除最新记录以外的所有关系

Updating relations using script overwrites relations for all but newest record

我有一个样式(服装)模型和一个图像模型以及一对多样式<=图像关系。

我有一个 Cloudinary jQuery 小部件来上传图像,然后我试图更新图像与图像的 URL 关系。目的是在 table.

中显示这些图像

但是,当我使用我的代码更新关系时,它只保存针对关系的最新 StyleCode。

因此,如果我保存一张图像,然后再保存另一张图像,图像 table 来自:

Image URL         Style
http://abc        1234

至此

Image URL         Style
http://abc        <blank>
http://xyz        1234  

我的第一个问题是,这种关系实际上是如何运作的?它似乎依赖于我的 StyleCode 来保持记录关系。我还以为是_key.....????

其次,您是否可以在我的代码中发现任何可能会覆盖以前的 StyleCode 的内容?

服务器端代码

function saveImageToStyle(images, styleCode) {
  var imgs = [];
  images.forEach(function(image)
  {
    var imageRecord = app.models.Images.newRecord();
    imageRecord.ThumbnailURL = image.thumbnail_url;
    imageRecord.ImageURL = image.url;
    imageRecord.Path = image.path;
    imageRecord.ImageName = image.original_filename;
    imgs.push(imageRecord);
  }); 
  app.saveRecords(imgs);
  var query = app.models.Styles.newQuery();
  query.filters.StyleCode._equals = styleCode; // is it this???
  var styleRecord = query.run()[0];
  styleRecord.Images = imgs;
  app.saveRecords([styleRecord]);
}

客户端代码

function saveStyleImages(images) {
  var styleCode = app.datasources.Styles.item.StyleCode; // is there a better way to get the current StyleCode?
  var status = app.pages.StyleEdit.descendants.Status; 
  google.script.run
  .withFailureHandler(function(error) {
    status.text = error.message;
  })
  .withSuccessHandler(function(result) {
    status.text = images + "success";
  })
  .saveImageToStyle(images, styleCode);
}

看起来您可以在客户端上执行所有操作(如果您的安全模型没有任何限制):

function addStyleImage(newImage) {
  var newImageDs = app.datasources.Styles.relation.Images.modes.create;
  var newImage = newImageDs.item;

  newImage.ThumbnailURL = image.thumbnail_url;
  newImage.ImageURL = image.url;
  newImage.Path = image.path;
  newImage.ImageName = image.original_filename;

  newImageDs.createItem(function(record) {
     // do smth with new record
  });
}

使用这种方法,您将立即在客户端上看到新图像(如果图像关系绑定到 table/list/grid)。

阅读更多:

https://developers.google.com/appmaker/models/datasources#create_mode_datasource

以下行替换所有现有样式关联:
styleRecord.Images = imgs;

您可以将其替换为:
styleRecord.Images = styleRecord.Images.concat(imgs);