如何在客户端脚本文件中一次创建多个 items/records 或保存 item/record 数组
How to create multi items/records or save item/record array at one time in client script file
我想使用客户端脚本同时创建多条记录。这就是我正在做的事情:
var ceateDatasource = app.datasources.Reservation.modes.create;
var newItem = ceateDatasource.item;
newItem.User = user; //'eric'
newItem.Description = description; //'000'
newItem.Location_Lab_fk = lab.value.Id; //'T'
newItem.Area_fk = area.value.Id; //'L'
newItem.Equipment_fk = equipment.value.Id; //'S'
for(var i = 0 ; i < 3; i ++) {
newItem.Start_Date = startDate;
newItem.Start_Hours = '03';
newItem.Start_Minutes = '00';
newItem.End_Date = startDate;
newItem.End_Hours = '23';
newItem.End_Minutes = '30';
// Create the new item
ceateDatasource.createItem();
}
但我得到的结果是这样的:
创建了三个记录,但只有第一个有数据。其他两个记录的字段上有空值。我怎样才能做到这一点?
谢谢。
更新(2019-3-27):
我能够通过将所有内容放入 for 循环 块中来使其工作。但是,我还有一个问题。
有没有类似下面示例代码的方法?
var recordData = [Data1, Data2, Data3]
var ceateDatasource;
var newItem = new Array(recordData.length) ;
for(var i = 0 ; i < recordData.length; i ++) {
ceateDatasource = app.datasources.Reservation.modes.create;
newItem[i] = ceateDatasource.item;
newItem[i].User = recordData[i].user;
newItem[i].Description = recordData[i].Description;
newItem[i].Location_Lab_fk = recordData[i].Location_Lab_fk;
newItem[i].Area_fk = recordData[i].Area_fk;
newItem[i].Equipment_fk = recordData[i].Equipment_fk;
newItem[i].Start_Date = recordData[i].Start_Date;
newItem[i].Start_Hours = recordData[i].Start_Hours;
newItem[i].Start_Minutes = recordData[i].Start_Minutes;
newItem[i].End_Date = recordData[i].End_Date;
newItem[i].End_Hours = recordData[i].End_Hours;
newItem[i].End_Minutes = recordData[i].End_Minutes;
}
// Create the new item
ceateDatasource.createItem();
首先,它准备一个数组 'newItem' 并且只调用 'ceateDatasource.createItem()' 一次来保存所有新记录(或项目)。
我尝试使用这种方法,但它只保存最后一条记录 'newItem[3]'.
我需要在 'ceateDatasource.createItem()' 中编写回调函数,但 Google App Maker 总是显示警告 "Don't make functions within a loop"。那么,有没有什么方法可以调用'createItem()'一次保存多条记录呢?或者有没有像'array.push'这样的函数可以使用?
谢谢。
根据 AppMaker 的 official documentation:
A create datasource is a datasource used to create items in a particular data source. Its item property is always populated by a draft item which can be bound to or set programmatically.
您要做的是从同一个草稿项目中创建三个项目。这就是为什么你看到你得到的结果。如果要创建多个项目,则需要为每个项目创建一个草稿项目,因此您需要做的就是将所有代码放在 for 循环中。
for(var i = 0 ; i < 3; i ++) {
var ceateDatasource = app.datasources.Reservation.modes.create;
var newItem = ceateDatasource.item;
newItem.User = user; //'eric'
newItem.Description = description; //'000'
newItem.Location_Lab_fk = lab.value.Id; //'T'
newItem.Area_fk = area.value.Id; //'L'
newItem.Equipment_fk = equipment.value.Id; //'S'
newItem.Start_Date = startDate;
newItem.Start_Hours = '03';
newItem.Start_Minutes = '00';
newItem.End_Date = startDate;
newItem.End_Hours = '23';
newItem.End_Minutes = '30';
// Create the new item
ceateDatasource.createItem();
}
如果您想使用客户端脚本同时保存多条记录,那么您要找的就是Manual Save Mode。因此,您所要做的就是转到模型的数据源并单击复选框 "Manual Save Mode"。
然后使用与上面相同的代码。唯一的区别是为了将更改持久保存到服务器,您需要显式保存更改。所以你所要做的就是在 for loop 块之后添加以下内容:
app.datasources.Reservation.saveChanges(function(){
//TODO: Callback handler
});
我想使用客户端脚本同时创建多条记录。这就是我正在做的事情:
var ceateDatasource = app.datasources.Reservation.modes.create;
var newItem = ceateDatasource.item;
newItem.User = user; //'eric'
newItem.Description = description; //'000'
newItem.Location_Lab_fk = lab.value.Id; //'T'
newItem.Area_fk = area.value.Id; //'L'
newItem.Equipment_fk = equipment.value.Id; //'S'
for(var i = 0 ; i < 3; i ++) {
newItem.Start_Date = startDate;
newItem.Start_Hours = '03';
newItem.Start_Minutes = '00';
newItem.End_Date = startDate;
newItem.End_Hours = '23';
newItem.End_Minutes = '30';
// Create the new item
ceateDatasource.createItem();
}
但我得到的结果是这样的:
创建了三个记录,但只有第一个有数据。其他两个记录的字段上有空值。我怎样才能做到这一点?
谢谢。
更新(2019-3-27):
我能够通过将所有内容放入 for 循环 块中来使其工作。但是,我还有一个问题。
有没有类似下面示例代码的方法?
var recordData = [Data1, Data2, Data3]
var ceateDatasource;
var newItem = new Array(recordData.length) ;
for(var i = 0 ; i < recordData.length; i ++) {
ceateDatasource = app.datasources.Reservation.modes.create;
newItem[i] = ceateDatasource.item;
newItem[i].User = recordData[i].user;
newItem[i].Description = recordData[i].Description;
newItem[i].Location_Lab_fk = recordData[i].Location_Lab_fk;
newItem[i].Area_fk = recordData[i].Area_fk;
newItem[i].Equipment_fk = recordData[i].Equipment_fk;
newItem[i].Start_Date = recordData[i].Start_Date;
newItem[i].Start_Hours = recordData[i].Start_Hours;
newItem[i].Start_Minutes = recordData[i].Start_Minutes;
newItem[i].End_Date = recordData[i].End_Date;
newItem[i].End_Hours = recordData[i].End_Hours;
newItem[i].End_Minutes = recordData[i].End_Minutes;
}
// Create the new item
ceateDatasource.createItem();
首先,它准备一个数组 'newItem' 并且只调用 'ceateDatasource.createItem()' 一次来保存所有新记录(或项目)。 我尝试使用这种方法,但它只保存最后一条记录 'newItem[3]'.
我需要在 'ceateDatasource.createItem()' 中编写回调函数,但 Google App Maker 总是显示警告 "Don't make functions within a loop"。那么,有没有什么方法可以调用'createItem()'一次保存多条记录呢?或者有没有像'array.push'这样的函数可以使用?
谢谢。
根据 AppMaker 的 official documentation:
A create datasource is a datasource used to create items in a particular data source. Its item property is always populated by a draft item which can be bound to or set programmatically.
您要做的是从同一个草稿项目中创建三个项目。这就是为什么你看到你得到的结果。如果要创建多个项目,则需要为每个项目创建一个草稿项目,因此您需要做的就是将所有代码放在 for 循环中。
for(var i = 0 ; i < 3; i ++) {
var ceateDatasource = app.datasources.Reservation.modes.create;
var newItem = ceateDatasource.item;
newItem.User = user; //'eric'
newItem.Description = description; //'000'
newItem.Location_Lab_fk = lab.value.Id; //'T'
newItem.Area_fk = area.value.Id; //'L'
newItem.Equipment_fk = equipment.value.Id; //'S'
newItem.Start_Date = startDate;
newItem.Start_Hours = '03';
newItem.Start_Minutes = '00';
newItem.End_Date = startDate;
newItem.End_Hours = '23';
newItem.End_Minutes = '30';
// Create the new item
ceateDatasource.createItem();
}
如果您想使用客户端脚本同时保存多条记录,那么您要找的就是Manual Save Mode。因此,您所要做的就是转到模型的数据源并单击复选框 "Manual Save Mode"。
然后使用与上面相同的代码。唯一的区别是为了将更改持久保存到服务器,您需要显式保存更改。所以你所要做的就是在 for loop 块之后添加以下内容:
app.datasources.Reservation.saveChanges(function(){
//TODO: Callback handler
});