如何使用Realtime API构建和使用协作数据模型? [英] How to build and use a collaborative data model with Realtime API?

查看:90
本文介绍了如何使用Realtime API构建和使用协作数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照构建协作数据模型中介绍的步骤进行操作我最终得到了下面的代码:
$ b

  function onLoad(doc){
var Book = function(){};

var model = doc.getModel();
gapi.drive.realtime.custom.registerType(Book,'Book');
Book.prototype.title = gapi.drive.realtime.custom.collaborativeField('title');
Book.prototype.author = gapi.drive.realtime.custom.collaborativeField('author');
Book.prototype.isbn = gapi.drive.realtime.custom.collaborativeField('isbn');
Book.prototype.isCheckedOut = gapi.drive.realtime.custom.collaborativeField('isCheckedOut');
Book.prototype.reviews = gapi.drive.realtime.custom.collaborativeField('reviews');
var book = model.create('Book');
model.getRoot()。set('book',book);
book.addEventListener(gapi.drive.realtime.EventType.OBJECT_CHANGED,function(e){console.log(e);});
book.title ='莫比迪克';
book.author ='梅尔维尔,赫尔曼';
book.isbn ='978-1470178192';
book.isCheckedOut = false;
}

不幸的是,当执行此操作时,出现以下错误: var book = model.create('Book'); 被执行:

lockquote
未捕获的java.lang.IllegalArgumentException :未知类型名称Book



驱动器实时API错误:invalid_compound_operation:在同步块结束时打开复合操作 - 您是否忘记调用endCompoundOperation()?


我错过了什么?

解决方案

在加载文档之前注册自定义类型。



您需要在这里设置三个不同的部分:


  • 加载之前:注册自定义类型。

  • 在init上:每个文档只发生一次。这里你想要初始化每个文档应该有的数据模型。

  • 文件加载:每次加载时都会发生一次。这是你想设置事件监听器的地方。



将你的函数分成三部分,它应该可以工作。

I have tried to follow the step described at Build a Collaborative Data Model and I end up with the following code :

function onLoad(doc){
  var Book = function(){};

  var model = doc.getModel();
  gapi.drive.realtime.custom.registerType(Book, 'Book');
  Book.prototype.title = gapi.drive.realtime.custom.collaborativeField('title');
  Book.prototype.author = gapi.drive.realtime.custom.collaborativeField('author');
  Book.prototype.isbn = gapi.drive.realtime.custom.collaborativeField('isbn');
  Book.prototype.isCheckedOut = gapi.drive.realtime.custom.collaborativeField('isCheckedOut');
  Book.prototype.reviews = gapi.drive.realtime.custom.collaborativeField('reviews');
  var book = model.create('Book');
  model.getRoot().set('book', book);
  book.addEventListener(gapi.drive.realtime.EventType.OBJECT_CHANGED, function(e){console.log(e);});
  book.title = 'Moby Dick';
  book.author = 'Melville, Herman';
  book.isbn = '978-1470178192';
  book.isCheckedOut = false;
}

Unfortunately when executing this I get the following error when var book = model.create('Book'); is executed :

Uncaught java.lang.IllegalArgumentException: Unknown type name Book

Drive Realtime API Error: invalid_compound_operation: Open compound operation at end of synchronous block - did you forget to call endCompoundOperation()?

What do I miss ?

解决方案

You need to register the custom type before you load the document.

There are three different sections you want here:

  • Before you load: register the custom type.
  • On init: This happens exactly once per document. Here you want to be initializing the data model that every doc should have.
  • On file loaded: This happens once per load. This is where you want to be setting up event listeners, etc.

Split your function into those three parts, and it should work.

这篇关于如何使用Realtime API构建和使用协作数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆