From a1dba7fe1e60302b757ea8668fc222a4470c778e 2015-03-27 21:03:12 From: Jason Grout Date: 2015-03-27 21:03:12 Subject: [PATCH] Change custom serialization to use custom models, rather than transmitting the serializer name across the wire This separates the kernel and the js much more cleanly, and doesn't use as much space on the wire as well! --- diff --git a/IPython/html/static/widgets/js/init.js b/IPython/html/static/widgets/js/init.js index 9dde6b0..0ea0a25 100644 --- a/IPython/html/static/widgets/js/init.js +++ b/IPython/html/static/widgets/js/init.js @@ -14,18 +14,33 @@ define([ "widgets/js/widget_selection", "widgets/js/widget_selectioncontainer", "widgets/js/widget_string", -], function(widgetmanager, linkModels) { - for (var target_name in linkModels) { - if (linkModels.hasOwnProperty(target_name)) { - widgetmanager.WidgetManager.register_widget_model(target_name, linkModels[target_name]); +], function(widgetmanager) { + + + /** + * From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith + * Can be removed with the string endsWith function is implemented in major browsers + */ + var endsWith = function(target, searchString, position) { + var subjectString = target.toString(); + if (position === undefined || position > subjectString.length) { + position = subjectString.length; } - } + position -= searchString.length; + var lastIndex = subjectString.indexOf(searchString, position); + return lastIndex !== -1 && lastIndex === position; + }; - // Register all of the loaded views with the widget manager. - for (var i = 2; i < arguments.length; i++) { - for (var target_name in arguments[i]) { - if (arguments[i].hasOwnProperty(target_name)) { - widgetmanager.WidgetManager.register_widget_view(target_name, arguments[i][target_name]); + // Register all of the loaded models and views with the widget manager. + for (var i = 1; i < arguments.length; i++) { + var module = arguments[i]; + for (var target_name in module) { + if (module.hasOwnProperty(target_name)) { + if (endsWith(target_name, "View")) { + widgetmanager.WidgetManager.register_widget_view(target_name, module[target_name]); + } else if (endsWith(target_name, "Model")) { + widgetmanager.WidgetManager.register_widget_model(target_name, module[target_name]); + } } } } diff --git a/IPython/html/static/widgets/js/types.js b/IPython/html/static/widgets/js/types.js deleted file mode 100644 index f5d8c76..0000000 --- a/IPython/html/static/widgets/js/types.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) IPython Development Team. -// Distributed under the terms of the Modified BSD License. - -define([ - "base/js/utils" -], function(utils){ - return { - models: { - deserialize: function deserialize_models(value, model) { - /** - * Replace model ids with models recursively. - */ - var unpacked; - if ($.isArray(value)) { - unpacked = []; - _.each(value, function(sub_value, key) { - unpacked.push(deserialize_models(sub_value, model)); - }); - return Promise.all(unpacked); - } else if (value instanceof Object) { - unpacked = {}; - _.each(value, function(sub_value, key) { - unpacked[key] = deserialize_models(sub_value, model); - }); - return utils.resolve_promises_dict(unpacked); - } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { - // get_model returns a promise already - return model.widget_manager.get_model(value.slice(10, value.length)); - } else { - return Promise.resolve(value); - } - }, - /* We don't need a serializer since models automatically serialize to their UUIDs */ - /* courtesy of the model toJSON */ - }, - } -}); diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index c59d25f..a643ea0 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -32,7 +32,6 @@ define(["widgets/js/manager", this.state_lock = null; this.id = model_id; this.views = {}; - this.serializers = {}; this._resolve_received_state = {}; if (comm !== undefined) { @@ -146,23 +145,17 @@ define(["widgets/js/manager", var state = msg.content.data.state || {}; var buffer_keys = msg.content.data.buffers || []; var buffers = msg.buffers || []; - var metadata = msg.content.data.metadata || {}; - var i,k; for (var i=0; i