##// END OF EJS Templates
Rewrite widget syncing...
Jason Grout -
Show More
@@ -23,19 +23,6 b''
23 // elsewhere.
23 // elsewhere.
24 define(["underscore",
24 define(["underscore",
25 "backbone",
25 "backbone",
26 ], function (Underscore, Backbone) {
27
28 Backbone.sync = function (method, model, options) {
29 // Sync widget models to back-end.
30 //
31 // Backbone.sync method must be in widgetmanager.js file instead of
32 // widget.js so it can be overwritten for different contexts.
33 var result = model._handle_sync(method, options);
34 if (options.success) {
35 options.success(result);
36 }
37 };
38
39
26
40 //--------------------------------------------------------------------
27 //--------------------------------------------------------------------
41 // WidgetManager class
28 // WidgetManager class
@@ -168,10 +155,6 b''
168 output : handle_output,
155 output : handle_output,
169 clear_output : handle_clear_output,
156 clear_output : handle_clear_output,
170
157
171 status : function (msg) {
172 view.model._handle_status(msg, that.callbacks(view));
173 },
174
175 // Special function only registered by widget messages.
158 // Special function only registered by widget messages.
176 // Allows us to get the cell for a message so we know
159 // Allows us to get the cell for a message so we know
177 // where to add widgets if the code requires it.
160 // where to add widgets if the code requires it.
@@ -120,57 +120,71 b' function(WidgetManager, Underscore, Backbone){'
120 }
120 }
121 },
121 },
122
122
123 _handle_sync: function (method, options) {
123 callbacks: function(callbacks) {
124 // Custom syncronization logic.
124 // Create msg callbacks for a comm msg.
125 var model_json = this.toJSON();
125 var that = this;
126 var attr;
126 if (callbacks.iopub === undefined) {callbacks.iopub = {};}
127
127 callbacks.iopub.status = function (msg) {
128 // Only send updated state if the state hasn't been changed
128 that._handle_status(msg, callbacks);
129 // during an update.
129 }
130 if (this.comm !== undefined) {
130 return callbacks;
131 if (this.pending_msgs >= this.msg_throttle) {
131 },
132 // The throttle has been exceeded, buffer the current msg so
133 // it can be sent once the kernel has finished processing
134 // some of the existing messages.
135 if (this.msg_buffer === null) {
136 this.msg_buffer = $.extend({}, model_json); // Copy
137 }
138 for (attr in options.attrs) {
139 var value = this._pack_models(options.attrs[attr]);
140 if (this.key_value_lock === null || attr !== this.key_value_lock[0] || value !== this.key_value_lock[1]) {
141 this.msg_buffer[attr] = value;
142 }
143 }
144
132
145 } else {
133 sync: function (method, model, options) {
146 // We haven't exceeded the throttle, send the message like
134 var error = options.error || function() {console.error('Backbone sync error:', arguments);}
147 // normal. If this is a patch operation, just send the
135 if (this.comm === undefined) {
148 // changes.
136 error();
149 var send_json = model_json;
137 return false;
150 send_json = {};
138 }
151 for (attr in options.attrs) {
139
152 var value = this._pack_models(options.attrs[attr]);
140 var attrs = (method==='patch') ? options.attrs : model.toJSON(options);
153 if (this.key_value_lock === null || attr !== this.key_value_lock[0] || value !== this.key_value_lock[1]) {
141
154 send_json[attr] = value;
142 if (this.key_value_lock !== null) {
155 }
143 var k = this.key_value_lock[0];
156 }
144 var v = this.key_value_lock[1];
157
145 if (attrs[k]===v) {
158 var is_empty = true;
146 delete attrs[k];
159 for (var prop in send_json) if (send_json.hasOwnProperty(prop)) is_empty = false;
160 if (!is_empty) {
161 ++this.pending_msgs;
162 var data = {method: 'backbone', sync_data: send_json};
163 this.comm.send(data, options.callbacks);
164 }
165 }
147 }
166 }
148 }
149 if (_.size(attrs) == 0) {
150 error();
151 return false;
152 }
153 var callbacks = model.callbacks(options.callbacks || {});
154 if (this.pending_msgs >= this.msg_throttle) {
155 // The throttle has been exceeded, buffer the current msg so
156 // it can be sent once the kernel has finished processing
157 // some of the existing messages.
158
159 // combine updates if it is a 'patch' sync, otherwise replace updates
160 switch (method) {
161 case 'patch':
162 this.msg_buffer = _.extend(this.msg_buffer || {}, attrs);
163 break;
164 case 'update':
165 this.msg_buffer = attrs;
166 break;
167 default:
168 error();
169 return false;
170 }
171 this.msg_buffer_callbacks = callbacks;
172
173 } else {
174 // We haven't exceeded the throttle, send the message like
175 // normal. If this is a patch operation, just send the
176 // changes.
177 var data = {method: 'backbone', sync_data: attrs};
178 this.comm.send(data, callbacks);
179 this.pending_msgs++;
180 }
167
181
168 // Since the comm is a one-way communication, assume the message
182 // Since the comm is a one-way communication, assume the message
169 // arrived.
183 // arrived. Don't call success since we don't have a model back from the server
170 return model_json;
184 // this means we miss out on the 'sync' event.
171 },
185 },
172
186
173 push: function(callbacks) {
187 save_changes: function(callbacks) {
174 // Push this model's state to the back-end
188 // Push this model's state to the back-end
175 //
189 //
176 // This invokes a Backbone.Sync.
190 // This invokes a Backbone.Sync.
@@ -294,7 +308,7 b' function(WidgetManager, Underscore, Backbone){'
294 },
308 },
295
309
296 touch: function () {
310 touch: function () {
297 this.model.push(this.callbacks());
311 this.model.save_changes(this.callbacks());
298 },
312 },
299
313
300 });
314 });
General Comments 0
You need to be logged in to leave comments. Login now