##// END OF EJS Templates
Merge pull request #5023 from jdfreder/widget-arrays...
Brian E. Granger -
r15086:d790d39e merge
parent child Browse files
Show More
@@ -210,6 +210,15 b' function(WidgetManager, _, Backbone){'
210 // Replace models with model ids recursively.
210 // Replace models with model ids recursively.
211 if (value instanceof Backbone.Model) {
211 if (value instanceof Backbone.Model) {
212 return value.id;
212 return value.id;
213
214 } else if ($.isArray(value)) {
215 var packed = [];
216 var that = this;
217 _.each(value, function(sub_value, key) {
218 packed.push(that._pack_models(sub_value));
219 });
220 return packed;
221
213 } else if (value instanceof Object) {
222 } else if (value instanceof Object) {
214 var packed = {};
223 var packed = {};
215 var that = this;
224 var that = this;
@@ -217,6 +226,7 b' function(WidgetManager, _, Backbone){'
217 packed[key] = that._pack_models(sub_value);
226 packed[key] = that._pack_models(sub_value);
218 });
227 });
219 return packed;
228 return packed;
229
220 } else {
230 } else {
221 return value;
231 return value;
222 }
232 }
@@ -224,13 +234,22 b' function(WidgetManager, _, Backbone){'
224
234
225 _unpack_models: function(value) {
235 _unpack_models: function(value) {
226 // Replace model ids with models recursively.
236 // Replace model ids with models recursively.
227 if (value instanceof Object) {
237 if ($.isArray(value)) {
238 var unpacked = [];
239 var that = this;
240 _.each(value, function(sub_value, key) {
241 unpacked.push(that._unpack_models(sub_value));
242 });
243 return unpacked;
244
245 } else if (value instanceof Object) {
228 var unpacked = {};
246 var unpacked = {};
229 var that = this;
247 var that = this;
230 _.each(value, function(sub_value, key) {
248 _.each(value, function(sub_value, key) {
231 unpacked[key] = that._unpack_models(sub_value);
249 unpacked[key] = that._unpack_models(sub_value);
232 });
250 });
233 return unpacked;
251 return unpacked;
252
234 } else {
253 } else {
235 var model = this.widget_manager.get_model(value);
254 var model = this.widget_manager.get_model(value);
236 if (model) {
255 if (model) {
@@ -1,3 +1,33 b''
1 var xor = function (a, b) {return !a ^ !b;};
2 var isArray = function (a) {return toString.call(a) === "[object Array]" || toString.call(a) === "[object RuntimeArray]";};
3 var recursive_compare = function(a, b) {
4 // Recursively compare two objects.
5 var same = true;
6 same = same && !xor(a instanceof Object, b instanceof Object);
7 same = same && !xor(isArray(a), isArray(b));
8
9 if (same) {
10 if (a instanceof Object) {
11 for (var key in a) {
12 if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
13 same = false;
14 break;
15 }
16 }
17 for (var key in b) {
18 if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
19 same = false;
20 break;
21 }
22 }
23 } else {
24 return a === b;
25 }
26 }
27
28 return same;
29 }
30
1 // Test the widget framework.
31 // Test the widget framework.
2 casper.notebook_test(function () {
32 casper.notebook_test(function () {
3 var index;
33 var index;
@@ -21,6 +51,43 b' casper.notebook_test(function () {'
21 this.test.assert(this.evaluate(function() {
51 this.test.assert(this.evaluate(function() {
22 return IPython.notebook.kernel.widget_manager !== undefined;
52 return IPython.notebook.kernel.widget_manager !== undefined;
23 }), 'Notebook widget manager instantiated');
53 }), 'Notebook widget manager instantiated');
54
55 // Functions that can be used to test the packing and unpacking APIs
56 var that = this;
57 var test_pack = function (input) {
58 var output = that.evaluate(function(input) {
59 var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined);
60 var results = model._pack_models(input);
61 delete model;
62 return results;
63 }, {input: input});
64 that.test.assert(recursive_compare(input, output),
65 JSON.stringify(input) + ' passed through Model._pack_model unchanged');
66 };
67 var test_unpack = function (input) {
68 var output = that.evaluate(function(input) {
69 var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined);
70 var results = model._unpack_models(input);
71 delete model;
72 return results;
73 }, {input: input});
74 that.test.assert(recursive_compare(input, output),
75 JSON.stringify(input) + ' passed through Model._unpack_model unchanged');
76 };
77 var test_packing = function(input) {
78 test_pack(input);
79 test_unpack(input);
80 };
81
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']])
24 });
91 });
25
92
26 var textbox = {};
93 var textbox = {};
General Comments 0
You need to be logged in to leave comments. Login now