##// END OF EJS Templates
Keep a running diff instead of forcing a full state update
Jonathan Frederic -
Show More
@@ -32,7 +32,7 b' function(WidgetManager, _, Backbone){'
32 // An ID unique to this model.
32 // An ID unique to this model.
33 // comm : Comm instance (optional)
33 // comm : Comm instance (optional)
34 this.widget_manager = widget_manager;
34 this.widget_manager = widget_manager;
35 this._set_calls = 0;
35 this._buffered_state_diff = {};
36 this.pending_msgs = 0;
36 this.pending_msgs = 0;
37 this.msg_throttle = 3;
37 this.msg_throttle = 3;
38 this.msg_buffer = null;
38 this.msg_buffer = null;
@@ -138,8 +138,13 b' function(WidgetManager, _, Backbone){'
138
138
139 set: function(key, val, options) {
139 set: function(key, val, options) {
140 // Set a value.
140 // Set a value.
141 this._set_calls++;
141 var return_value = WidgetModel.__super__.set.apply(this, arguments);
142 return WidgetModel.__super__.set.apply(this, arguments);
142
143 // Backbone only remembers the diff of the most recent set()
144 // opertation. Calling set multiple times in a row results in a
145 // loss of diff information. Here we keep our own running diff.
146 this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {});
147 return return_value;
143 },
148 },
144
149
145 sync: function (method, model, options) {
150 sync: function (method, model, options) {
@@ -205,26 +210,14 b' function(WidgetManager, _, Backbone){'
205 // Since the comm is a one-way communication, assume the message
210 // Since the comm is a one-way communication, assume the message
206 // arrived. Don't call success since we don't have a model back from the server
211 // arrived. Don't call success since we don't have a model back from the server
207 // this means we miss out on the 'sync' event.
212 // this means we miss out on the 'sync' event.
208 this._set_calls = 0;
213 this._buffered_state_diff = {};
209 },
214 },
210
215
211 save_changes: function(callbacks) {
216 save_changes: function(callbacks) {
212 // Push this model's state to the back-end
217 // Push this model's state to the back-end
213 //
218 //
214 // This invokes a Backbone.Sync.
219 // This invokes a Backbone.Sync.
215
220 this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks});
216 // Backbone only remembers the diff of the most recent set()
217 // opertation. Calling set multiple times in a row results in a
218 // loss of diff information which means we need to send a full
219 // state. If diffing is important to the user, model.set(...) should
220 // only be called once prior to a view.touch(). If multiple
221 // parameters need to be set, use the model.set({key1: val1, key2: val2, ...})
222 // signature.
223 if (self._set_calls <= 1) {
224 this.save(this.changedAttributes(), {patch: true, callbacks: callbacks});
225 } else {
226 this.save(null, {patch: false, callbacks: callbacks});
227 }
228 },
221 },
229
222
230 _pack_models: function(value) {
223 _pack_models: function(value) {
General Comments 0
You need to be logged in to leave comments. Login now