有没有一种方法可以加载一个引用的对象,而不需要去掉类名?
例如,在我的应用程序中,我经常从引用的类中加载数据。
所以我有一个引用这个的数据对象: /db/Shifts/73c81cc9-fa14-4fbe-9839-10c4121b3fc6
return db.Shifts.load('73c81cc9-fa14-4fbe-9839-10c4121b3fc6')是加载引用所需要的,所以我做了很多这样的事情:
var cleanID = obj.ShiftID.replace('/db/Shifts/','');
return db.Shifts.load(cleanID)有没有更好的方法来做这件事?是像这样吗?
return db.load('/db/Shifts/73c81cc9-fa14-4fbe-9839-10c4121b3fc6')发布于 2017-12-27 18:18:11
是的,有很多方法可以解决你的问题。
可以通过id (/db/Shifts/73c81cc9-fa14-4fbe-9839- 10c4121b3fc6)或键(73c81cc9-fa14-4fbe-9839-10c4121b3fc6)加载对象,load方法同时支持这两种方式。
// resolves both to the same object
db.Shifts.load('/db/Shifts/73c81cc9-fa14-4fbe-9839-10c4121b3fc6')
db.Shifts.load('73c81cc9-fa14-4fbe-9839-10c4121b3fc6')您可以使用相应的访问器直接从任何对象引用访问id或键:
例如,您有一个对象obj,它具有对Shift实例的引用shift。然后,您可以轻松地直接访问引用的id或键。
obj.shift.id == '/db/Shifts/73c81cc9-fa14-4fbe-9839-10c4121b3fc6'
obj.shift.key == '73c81cc9-fa14-4fbe-9839-10c4121b3fc6'如果要加载引用,可以直接使用引用load方法:
obj.shift.load().then(shift => {
shift.property = 'name';
// Note that the obj.shift reference is resolved by the load call
obj.shift === shift;
return shift.save(); //do whatever you want to do with the reference
})在我们的object references指南中对此进行了描述。
使用引用的shift对象直接加载对象的另一种方法是deep loading,您可以使用深度加载通过一次调用加载具有其引用的对象:
// The depth: 1 parameter ensures that all directly referenced objects of obj
// will be resolved by the load call
DB.MyClassWithShiftReference.load(id, {depth: 1}).then(obj => {
obj.shift.property = 'name';
})https://stackoverflow.com/questions/47963402
复制相似问题