Show More
@@ -32,6 +32,7 b' function(WidgetManager, _, Backbone){' | |||
|
32 | 32 | // An ID unique to this model. |
|
33 | 33 | // comm : Comm instance (optional) |
|
34 | 34 | this.widget_manager = widget_manager; |
|
35 | this._buffered_state_diff = {}; | |
|
35 | 36 | this.pending_msgs = 0; |
|
36 | 37 | this.msg_throttle = 3; |
|
37 | 38 | this.msg_buffer = null; |
@@ -93,7 +94,7 b' function(WidgetManager, _, Backbone){' | |||
|
93 | 94 | _.each(state, function(value, key) { |
|
94 | 95 | that.key_value_lock = [key, value]; |
|
95 | 96 | try { |
|
96 |
|
|
|
97 | WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]); | |
|
97 | 98 | } finally { |
|
98 | 99 | that.key_value_lock = null; |
|
99 | 100 | } |
@@ -135,6 +136,17 b' function(WidgetManager, _, Backbone){' | |||
|
135 | 136 | return callbacks; |
|
136 | 137 | }, |
|
137 | 138 | |
|
139 | set: function(key, val, options) { | |
|
140 | // Set a value. | |
|
141 | var return_value = WidgetModel.__super__.set.apply(this, arguments); | |
|
142 | ||
|
143 | // Backbone only remembers the diff of the most recent set() | |
|
144 | // operation. 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; | |
|
148 | }, | |
|
149 | ||
|
138 | 150 | sync: function (method, model, options) { |
|
139 | 151 | // Handle sync to the back-end. Called when a model.save() is called. |
|
140 | 152 | |
@@ -158,6 +170,7 b' function(WidgetManager, _, Backbone){' | |||
|
158 | 170 | } |
|
159 | 171 | |
|
160 | 172 | // Only sync if there are attributes to send to the back-end. |
|
173 | attrs = this._pack_models(attrs); | |
|
161 | 174 | if (_.size(attrs) > 0) { |
|
162 | 175 | |
|
163 | 176 | // If this message was sent via backbone itself, it will not |
@@ -197,13 +210,14 b' function(WidgetManager, _, Backbone){' | |||
|
197 | 210 | // Since the comm is a one-way communication, assume the message |
|
198 | 211 | // arrived. Don't call success since we don't have a model back from the server |
|
199 | 212 | // this means we miss out on the 'sync' event. |
|
213 | this._buffered_state_diff = {}; | |
|
200 | 214 | }, |
|
201 | 215 | |
|
202 | 216 | save_changes: function(callbacks) { |
|
203 | 217 | // Push this model's state to the back-end |
|
204 | 218 | // |
|
205 | 219 | // This invokes a Backbone.Sync. |
|
206 |
this.save(this. |
|
|
220 | this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks}); | |
|
207 | 221 | }, |
|
208 | 222 | |
|
209 | 223 | _pack_models: function(value) { |
@@ -8,13 +8,14 b' var recursive_compare = function(a, b) {' | |||
|
8 | 8 | |
|
9 | 9 | if (same) { |
|
10 | 10 | if (a instanceof Object) { |
|
11 |
|
|
|
11 | var key; | |
|
12 | for (key in a) { | |
|
12 | 13 | if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { |
|
13 | 14 | same = false; |
|
14 | 15 | break; |
|
15 | 16 | } |
|
16 | 17 | } |
|
17 |
for ( |
|
|
18 | for (key in b) { | |
|
18 | 19 | if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { |
|
19 | 20 | same = false; |
|
20 | 21 | break; |
@@ -26,7 +27,7 b' var recursive_compare = function(a, b) {' | |||
|
26 | 27 | } |
|
27 | 28 | |
|
28 | 29 | return same; |
|
29 | } | |
|
30 | }; | |
|
30 | 31 | |
|
31 | 32 |
|
|
32 | 33 | casper.notebook_test(function () { |
@@ -58,7 +59,6 b' casper.notebook_test(function () {' | |||
|
58 | 59 | var output = that.evaluate(function(input) { |
|
59 | 60 | var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined); |
|
60 | 61 | var results = model._pack_models(input); |
|
61 | delete model; | |
|
62 | 62 | return results; |
|
63 | 63 | }, {input: input}); |
|
64 | 64 | that.test.assert(recursive_compare(input, output), |
@@ -68,7 +68,6 b' casper.notebook_test(function () {' | |||
|
68 | 68 | var output = that.evaluate(function(input) { |
|
69 | 69 | var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined); |
|
70 | 70 | var results = model._unpack_models(input); |
|
71 | delete model; | |
|
72 | 71 | return results; |
|
73 | 72 | }, {input: input}); |
|
74 | 73 | that.test.assert(recursive_compare(input, output), |
@@ -79,15 +78,64 b' casper.notebook_test(function () {' | |||
|
79 | 78 | test_unpack(input); |
|
80 | 79 | }; |
|
81 | 80 | |
|
82 | test_packing({0: 'hi', 1: 'bye'}) | |
|
83 | test_packing(['hi', 'bye']) | |
|
84 | test_packing(['hi', 5]) | |
|
85 | test_packing(['hi', '5']) | |
|
86 | test_packing([1.0, 0]) | |
|
87 | test_packing([1.0, false]) | |
|
88 | test_packing([1, false]) | |
|
89 | test_packing([1, false, {a: 'hi'}]) | |
|
90 | test_packing([1, false, ['hi']]) | |
|
81 | test_packing({0: 'hi', 1: 'bye'}); | |
|
82 | test_packing(['hi', 'bye']); | |
|
83 | test_packing(['hi', 5]); | |
|
84 | test_packing(['hi', '5']); | |
|
85 | test_packing([1.0, 0]); | |
|
86 | test_packing([1.0, false]); | |
|
87 | test_packing([1, false]); | |
|
88 | test_packing([1, false, {a: 'hi'}]); | |
|
89 | test_packing([1, false, ['hi']]); | |
|
90 | ||
|
91 | // Test multi-set, single touch code. First create a custom widget. | |
|
92 | this.evaluate(function() { | |
|
93 | var MultiSetView = IPython.DOMWidgetView.extend({ | |
|
94 | render: function(){ | |
|
95 | this.model.set('a', 1); | |
|
96 | this.model.set('b', 2); | |
|
97 | this.model.set('c', 3); | |
|
98 | this.touch(); | |
|
99 | }, | |
|
100 | }); | |
|
101 | IPython.WidgetManager.register_widget_view('MultiSetView', MultiSetView); | |
|
102 | }, {}); | |
|
103 | }); | |
|
104 | ||
|
105 | // Try creating the multiset widget, verify that sets the values correctly. | |
|
106 | var multiset = {}; | |
|
107 | multiset.index = this.append_cell( | |
|
108 | 'from IPython.utils.traitlets import Unicode, CInt\n' + | |
|
109 | 'class MultiSetWidget(widgets.Widget):\n' + | |
|
110 | ' _view_name = Unicode("MultiSetView", sync=True)\n' + | |
|
111 | ' a = CInt(0, sync=True)\n' + | |
|
112 | ' b = CInt(0, sync=True)\n' + | |
|
113 | ' c = CInt(0, sync=True)\n' + | |
|
114 | ' d = CInt(-1, sync=True)\n' + // See if it sends a full state. | |
|
115 | ' def _handle_receive_state(self, sync_data):\n' + | |
|
116 | ' widgets.Widget._handle_receive_state(self, sync_data)\n'+ | |
|
117 | ' self.d = len(sync_data)\n' + | |
|
118 | 'multiset = MultiSetWidget()\n' + | |
|
119 | 'display(multiset)\n' + | |
|
120 | 'print(multiset.model_id)'); | |
|
121 | this.execute_cell_then(multiset.index, function(index) { | |
|
122 | multiset.model_id = this.get_output_cell(index).text.trim(); | |
|
123 | }); | |
|
124 | ||
|
125 | this.wait_for_widget(multiset); | |
|
126 | ||
|
127 | index = this.append_cell( | |
|
128 | 'print("%d%d%d" % (multiset.a, multiset.b, multiset.c))'); | |
|
129 | this.execute_cell_then(index, function(index) { | |
|
130 | this.test.assertEquals(this.get_output_cell(index).text.trim(), '123', | |
|
131 | 'Multiple model.set calls and one view.touch update state in back-end.'); | |
|
132 | }); | |
|
133 | ||
|
134 | index = this.append_cell( | |
|
135 | 'print("%d" % (multiset.d))'); | |
|
136 | this.execute_cell_then(index, function(index) { | |
|
137 | this.test.assertEquals(this.get_output_cell(index).text.trim(), '3', | |
|
138 | 'Multiple model.set calls sent a partial state.'); | |
|
91 | 139 | }); |
|
92 | 140 | |
|
93 | 141 | var textbox = {}; |
General Comments 0
You need to be logged in to leave comments.
Login now