drive realtime api model toJson not populating fields in custom type -
i'm building drive realtime project using custom types: https://developers.google.com/google-apps/realtime/custom-objects.
i'm having issue fields in custom objects not exported in model.tojson() output. i'm sure i'm missing something, haven't been able find differences how i'm constructing custom object vs. realtime playground or realtime api documentation.
sample repro case using realtime playground below.
1) go realtime playground: https://realtimeplayground.appspot.com/ 2) open developer console 3) run following code
test = function () {} test.prototype = { init: function() { this.name = 'testname';}}; test.prototype.name = gapi.drive.realtime.custom.collaborativefield('name'); gapi.drive.realtime.custom.registertype(test, 'testtype') gapi.drive.realtime.custom.setinitializer(test, test.prototype.init); var model = window.doc.getmodel() model.getroot().set('mytest', model.create(test)); model.tojson()
observed output:
"{"id":"root","type":"map","value": {"demo_string": {"id":"tq50c9iybcxi","type":"editablestring","value":"edit me!"}, "demo_list":{"id":"zmjcloeubcxj","type":"list","value": [{"json":"cat"},{"json":"dog"},{"json":"sheep"},{"json":"chicken"}]}, "demo_cursors":{"id":"6tj6zzd2bcxj","type":"map","value":{}}, "demo_map":{"id":"ukrrmphbbcxj","type":"map","value": {"key1":{"json":"value 1"},"key2":{"json":"value 2"},"key3":{"json":"value 3"}}}, "demo_custom":{"id":"44nsumapbcxk","type":"demomovie","value": {"name":{"json":"minority report"}, "director":{"json":"steven spielberg"}, "notes":{"json":""},"rating":{"json":""}}}, "mytest":{"id":"kq4hcv4ubcvw","type":"testtype","value":{}}}}"
expected:
"{"id":"root","type":"map","value": {"demo_string": {"id":"tq50c9iybcxi","type":"editablestring","value":"edit me!"}, "demo_list":{"id":"zmjcloeubcxj","type":"list","value": [{"json":"cat"},{"json":"dog"},{"json":"sheep"},{"json":"chicken"}]}, "demo_cursors":{"id":"6tj6zzd2bcxj","type":"map","value":{}}, "demo_map":{"id":"ukrrmphbbcxj","type":"map","value": {"key1":{"json":"value 1"},"key2":{"json":"value 2"},"key3":{"json":"value 3"}}}, "demo_custom":{"id":"44nsumapbcxk","type":"demomovie","value": {"name":{"json":"minority report"}, "director":{"json":"steven spielberg"}, "notes":{"json":""},"rating":{"json":""}}}, "mytest":{"id":"kq4hcv4ubcvw","type":"testtype","value":{"json":"testname"}}}}}"
registering custom types can occur during "pre-load" phase of document life cycle. code correct, being executed on document after document has loaded. causes custom object not constructed, why lacking json value have specified in init function.
to see correct flow in action, put break point on line 88 of static/elements/playground-app.js in realtime playground application. refresh page , when execution has paused, run code console:
test = function () {} test.prototype = { init: function() { this.name = 'testname';}}; test.prototype.name = gapi.drive.realtime.custom.collaborativefield('name'); gapi.drive.realtime.custom.registertype(test, 'testtype') gapi.drive.realtime.custom.setinitializer(test, test.prototype.init);
resume execution. when application has loaded, run rest of code:
var model = window.doc.getmodel() model.getroot().set('mytest', model.create(test)); model.tojson()
you see outputted json matches expecting:
"mytest":{"id":"1oiqd2qoeqbs","type":"testtype","value":{"name":{"json":"testname"}}}
this workflow documented under "registering custom types , fields" title of custom collaborative objects guide.
Comments
Post a Comment