How to build and use a collaborative data model with Realtime API?

旧时模样 提交于 2019-12-01 07:40:22

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/16178447/how-to-build-and-use-a-collaborative-data-model-with-realtime-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!