##// END OF EJS Templates
Rewrite widget syncing...
Jason Grout -
Show More
@@ -23,19 +23,6
23 23 // elsewhere.
24 24 define(["underscore",
25 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 28 // WidgetManager class
@@ -168,10 +155,6
168 155 output : handle_output,
169 156 clear_output : handle_clear_output,
170 157
171 status : function (msg) {
172 view.model._handle_status(msg, that.callbacks(view));
173 },
174
175 158 // Special function only registered by widget messages.
176 159 // Allows us to get the cell for a message so we know
177 160 // where to add widgets if the code requires it.
@@ -120,57 +120,71 function(WidgetManager, Underscore, Backbone){
120 120 }
121 121 },
122 122
123 _handle_sync: function (method, options) {
124 // Custom syncronization logic.
125 var model_json = this.toJSON();
126 var attr;
123 callbacks: function(callbacks) {
124 // Create msg callbacks for a comm msg.
125 var that = this;
126 if (callbacks.iopub === undefined) {callbacks.iopub = {};}
127 callbacks.iopub.status = function (msg) {
128 that._handle_status(msg, callbacks);
129 }
130 return callbacks;
131 },
127 132
128 // Only send updated state if the state hasn't been changed
129 // during an update.
130 if (this.comm !== undefined) {
133 sync: function (method, model, options) {
134 var error = options.error || function() {console.error('Backbone sync error:', arguments);}
135 if (this.comm === undefined) {
136 error();
137 return false;
138 }
139
140 var attrs = (method==='patch') ? options.attrs : model.toJSON(options);
141
142 if (this.key_value_lock !== null) {
143 var k = this.key_value_lock[0];
144 var v = this.key_value_lock[1];
145 if (attrs[k]===v) {
146 delete attrs[k];
147 }
148 }
149 if (_.size(attrs) == 0) {
150 error();
151 return false;
152 }
153 var callbacks = model.callbacks(options.callbacks || {});
131 154 if (this.pending_msgs >= this.msg_throttle) {
132 155 // The throttle has been exceeded, buffer the current msg so
133 156 // it can be sent once the kernel has finished processing
134 157 // 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 }
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;
143 170 }
171 this.msg_buffer_callbacks = callbacks;
144 172
145 173 } else {
146 174 // We haven't exceeded the throttle, send the message like
147 175 // normal. If this is a patch operation, just send the
148 176 // changes.
149 var send_json = model_json;
150 send_json = {};
151 for (attr in options.attrs) {
152 var value = this._pack_models(options.attrs[attr]);
153 if (this.key_value_lock === null || attr !== this.key_value_lock[0] || value !== this.key_value_lock[1]) {
154 send_json[attr] = value;
155 }
156 }
157
158 var is_empty = true;
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 }
177 var data = {method: 'backbone', sync_data: attrs};
178 this.comm.send(data, callbacks);
179 this.pending_msgs++;
166 180 }
167 181
168 182 // Since the comm is a one-way communication, assume the message
169 // arrived.
170 return model_json;
183 // arrived. Don't call success since we don't have a model back from the server
184 // this means we miss out on the 'sync' event.
171 185 },
172 186
173 push: function(callbacks) {
187 save_changes: function(callbacks) {
174 188 // Push this model's state to the back-end
175 189 //
176 190 // This invokes a Backbone.Sync.
@@ -294,7 +308,7 function(WidgetManager, Underscore, Backbone){
294 308 },
295 309
296 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