0

有没有办法加载一个引用的对象而不得不去掉类名?

例如,在我的应用程序中,我经常从引用的类中加载数据。

所以我有一个引用这个的数据对象:/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')
4

1 回答 1

1

是的,有很多方法可以解决您的问题。

您可以通过 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了 Shifts 实例。然后您可以轻松地直接访问引用的 id 或 key。

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

它在我们的对象引用指南中进行了描述。

另一种直接使用引用的 shift 对象加载对象的方法是深度加载您可以使用深度加载通过一次调用来加载对象及其引用:

// 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';
})
于 2017-12-27T10:18:10.637 回答