2017-05-31 60 views
0

我有一个样式(服装)模型和图像模型和一对多样式< =图像的关系。更新关系将覆盖所有的关系,而且最新的记录

我有一个jQuery的Cloudinary部件上传图片,然后我试图更新的URL图像的图像关系。然后打算将这些图像显示在表格中。

然而,当我用我的代码更新的关系,它不仅节省了对关系的最新StyleCode。

所以,如果我救一个图像,然后又一个,图像表云从这个:

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); 
} 

回答

0

下面一行替换所有现有的样式协会: styleRecord.Images = IMGS; 您可以将其替换为: styleRecord.Images = styleRecord.Images.concat(imgs);

+0

谢谢!我styleRecord.Images.push做到了(IMGS):) – Samuurai

+0

从谷歌员工的官员说,修复你需要做的是这样的: 为(VAR I = 0; I Samuurai

1

貌似可以在客户端上做的一切(如果你没有任何限制与您的安全模式):

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 
    }); 
} 

使用此方法,您将立即在客户端看到新图像(如果Images关系绑定到表/列表/网格)。

了解更多:

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

+0

从来不知道我可以在客户端做到这一切。将尝试!谢谢! – Samuurai

+0

非常欢迎您!如果它能帮助你的上午同志,请不要忘记接受答案。 –