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)
| r475 | r513 | |
|---|---|---|
| 26 | 26 | return isNaN(value) ? null : value; |
| 27 | 27 | }, |
| 28 | 28 | |
| 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) | |
| 30 | 31 | appendText: function(element, text) { |
| 31 | 32 | element = $(element); |
| 32 | text = String.interpret(text); | |
| 33 | element.appendChild(document.createTextNode(text)); | |
| 33 | element.appendChild(document.createTextNode(String.interpret(text))); | |
| 34 | 34 | return element; |
| 35 | 35 | } |
| 36 | 36 | }); |
| 37 | 37 | |
| 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 | })(); | |
| 44 | 49 | |
| 45 | 50 | Object.extend(document.viewport, { |
| 46 | 51 | // Alias this method for consistency |
trunk/test/unit/base_test.html (Unified diff)
| r475 | r513 | |
|---|---|---|
| 76 | 76 | } |
| 77 | 77 | }); |
| 78 | 78 | |
| 79 | var documentReady = false; | |
| 79 | var documentReady = [ ]; | |
| 80 | 80 | |
| 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) }); | |
| 82 | 84 | |
| 83 | 85 | new Test.Unit.Runner({ |
| 84 | 86 | testArrayEmpty: function() { with(this) { |
| --- | --- | |
| 87 | 89 | }}, |
| 88 | 90 | |
| 89 | 91 | 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()); | |
| 93 | 111 | }}, |
| 94 | 112 | |
| 95 | 113 | testArrayRemoveAt: function(){with(this){ |
| --- | --- | |
| 109 | 127 | array.remove(3); |
| 110 | 128 | assertEnumEqual([ 1, 2, 4, 5 ], array); |
| 111 | 129 | |
| 112 | var object = new Object; | |
| 130 | var object = { }; | |
| 113 | 131 | array.push(object); |
| 114 | 132 | array.push(object); |
| 115 | 133 | array.remove(object); |
| --- | --- | |
| 248 | 266 | }}, |
| 249 | 267 | |
| 250 | 268 | 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"); | |
| 251 | 272 | var receiver; |
| 252 | 273 | 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 | }); | |
| 255 | 279 | }} |
| 256 | 280 | }, "testlog"); |
| 257 | 281 | // ]]> |


RSS feeds