Changeset 513

* Ensure document.whenReady callbacks are called in register order
* Defer document.whenReady callbacks when document already loaded
* Better tests for Array#insert

Feb 08 2008 * 11:05 (11 months ago)
Committed by sam

Affected files:

trunk/src/core/dom.js (Unified diff)

r475r513
2626 return isNaN(value) ? null : value;
2727 },
2828
29 // by Tobie Langel (http://tobielangel.com/2007/5/22/prototype-quick-tip)
29 // with courtesy of Tobie Langel
30 // (http://tobielangel.com/2007/5/22/prototype-quick-tip)
3031 appendText: function(element, text) {
3132 element = $(element);
32 text = String.interpret(text);
33 element.appendChild(document.createTextNode(text));
33 element.appendChild(document.createTextNode(String.interpret(text)));
3434 return element;
3535 }
3636 });
3737
38 document.whenReady = function(callback) {
39 if (document.loaded)
40 callback.call(document);
41 else
42 document.observe('dom:loaded', callback);
43 };
38 document.whenReady = (function() {
39 var callbacks = [ ];
40
41 document.observe('dom:loaded', function() {
42 callbacks.invoke('call', document);
43 });
44
45 return function(callback) {
46 document.loaded ? callback.bind(document).defer() : callbacks.push(callback);
47 }
48 })();
4449
4550 Object.extend(document.viewport, {
4651 // Alias this method for consistency

trunk/test/unit/base_test.html (Unified diff)

r475r513
7676 }
7777 });
7878
79 var documentReady = false;
79 var documentReady = [ ];
8080
81 document.whenReady(function() { documentReady = true });
81 document.whenReady(function() { documentReady.push(this) });
82 document.whenReady(function() { documentReady.push(2) });
83 document.whenReady(function() { documentReady.push(3) });
8284
8385 new Test.Unit.Runner({
8486 testArrayEmpty: function() { with(this) {
------
8789 }},
8890
8991 testArrayInsert: function() { with(this) {
90 var array = $R(1, 3).toArray();
91 assertIdentical(array, array.insert(2, "foo", "bar"), "Array#insert should return instance");
92 assertEnumEqual([ 1, 2, 'foo', 'bar', 3 ], array);
92 var array = $R(1, 4).toArray();
93 assertIdentical(array, array.insert(3, 'PI'), 'Array#insert should return instance');
94 assertEnumEqual([ 1, 2, 3, 'PI', 4 ], array);
95
96 array = [ ];
97 array.insert(0, 'hello', 'world');
98 assertEnumEqual([ 'hello', 'world' ], array);
99
100 array.insert(-1, '!');
101 assertEnumEqual([ 'hello', 'world', '!' ], array);
102 array.insert(-3, 'wonderful', 'prototypish');
103 assertEnumEqual([ 'hello', 'wonderful', 'prototypish', 'world', '!' ], array);
104
105 array = [ ];
106 array.insert(10, "filled");
107 assertEqual(11, array.length);
108 assertEqual("filled", array[10]);
109 assertUndefined(array[0]);
110 assertEnumEqual(["filled"], array.compact());
93111 }},
94112
95113 testArrayRemoveAt: function(){with(this){
------
109127 array.remove(3);
110128 assertEnumEqual([ 1, 2, 4, 5 ], array);
111129
112 var object = new Object;
130 var object = { };
113131 array.push(object);
114132 array.push(object);
115133 array.remove(object);
------
248266 }},
249267
250268 testDocumentWhenReady: function(){with(this){
269 assertEqual(3, documentReady.length);
270 assertIdentical(document, documentReady.shift(), "callback should be bound to document");
271 assertEnumEqual([ 2, 3 ], documentReady, "callbacks aren't called in register order");
251272 var receiver;
252273 document.whenReady(function() { receiver = this });
253 assert(receiver, "callback doesn't get called when document is already loaded");
254 assertIdentical(document, receiver, "callback should be bound to document");
274 assertUndefined(receiver, "callback should be defered when document is already loaded");
275 wait(100, function() {
276 assert(receiver, "callback doesn't get called when document is already loaded");
277 assertIdentical(document, receiver, "callback should be bound to document");
278 });
255279 }}
256280 }, "testlog");
257281 // ]]>