当indexReference仅在包含在对象中时存储在协作映射中时,Google Realtime循环JSON错误

时间:2015-02-13 18:09:12

标签: json google-drive-api google-drive-realtime-api

尝试存储每个用户的indexReferences,我发现当我将一个(或多个)直接存储在地图中时,它可以正常工作。但是,当存储在对象(或自定义实时对象)中时,实时API会生成循环JSON错误。

这很好用:

function doRegisterTypes() {
    gapi.drive.realtime.custom.registerType(MyCustomType, "MyCustomType");
    MyCustomType.prototype.startPoints = gapi.drive.realtime.custom.collaborativeField('startPoints');
    MyCustomType.prototype.endPoints = gapi.drive.realtime.custom.collaborativeField('endPoints');
    MyCustomType.prototype.elements = gapi.drive.realtime.custom.collaborativeField('elements');

    gapi.drive.realtime.custom.setInitializer(MyCustomType, initializeMyCustomType);
}

function initializeMyCustomType() {
    var model = gapi.drive.realtime.custom.getModel(this);
    this.startPoints = model.createMap();
    this.endPoints = model.createMap();
    this.elements = model.createList();
}

function initializeModel(model) {
    var o = model.create("MyCustomType");
    o.elements.pushAll(["foo", "bar"]);
    var startIndex = o.elements.registerReference(0, false);
    var endIndex = o.elements.registerReference(0, false);
    o.startPoints.set(UserId, startIndex);
    o.endPoints.set(UserId, endIndex);

    model.getRoot().set("MyCustomObject", o);
}

但是这没有,在将范围对象存储在地图中时失败了循环JSON错误:

function doRegisterTypes() {
    gapi.drive.realtime.custom.registerType(MyCustomType, "MyCustomType");
    MyCustomType.prototype.ranges = gapi.drive.realtime.custom.collaborativeField('ranges');
    MyCustomType.prototype.elements = gapi.drive.realtime.custom.collaborativeField('elements');

    gapi.drive.realtime.custom.setInitializer(MyCustomType, initializeMyCustomType);
}

function initializeMyCustomType() {
    var model = gapi.drive.realtime.custom.getModel(this);
    this.ranges = model.createMap();
    this.elements = model.createList();
}

function initializeModel(model) {
    var o = model.create("MyCustomType");
    o.elements.pushAll(["foo", "bar"]);
    var startIndex = o.elements.registerReference(0, false);
    var endIndex = o.elements.registerReference(0, false);

    // FAILS:
    o.ranges.set(UserId, {start:startIndex, end:endIndex});

    model.getRoot().set("MyCustomObject", o);
}

我应该强调单个indexReference出现的错误,以及该对象是否是特定的自定义类型,以及WHENEVER将值设置到map中:初始化模型或更高版本。这就好像indexReferences不能存储在除“顶级”之外的任何东西之外,尽管这没什么意义。

功能?错误?用户stoopidity?

1 个答案:

答案 0 :(得分:1)

您无法在CollaborativeObject中的任意json中存储CollaborativeObjects。 CollaborativeObjects(包括IndexReferences)必须直接存储在其他CollaborativeObjects中。

(这有几个原因,主要与协作的工作方式有关.json对象被视为任意blob,其内容被忽略。)

在这种情况下,您可以创建一个Range自定义对象类型,该类型具有开始和结束CollaborativeField。 (或带有2个元素的CollaborativeList ..)