Confirmed users
478
edits
(→Sample) |
|||
(5 intermediate revisions by 3 users not shown) | |||
Line 13: | Line 13: | ||
=== Stage.1 - Painless instantiation === | === Stage.1 - Painless instantiation === | ||
'''Everything that has states need to be an instance, even if we only need one in the app.''' Rewrite all modules to be instantiable and rewrite unit tests, as well as JSDOC. | |||
See [[Gaia/System/Refactoring_Plan#Stage_1_example|Stage 1 example]] for detail. | |||
There are already bugs (87 bugs, excluding downloadmananger and fxaccounts for now. Take as you like!) of this stage for every js under the system app. See meta bug. | |||
==== Initialization ==== | |||
'''We should not change where we initialize the instance of the module in this stage.''' We do this in Stage 1 to ensure we don't don't need to resolve the launching sequence puzzle for now. If the module your are dealing with simply start itself, feel free to add | |||
<pre> | <pre> | ||
/** | |||
/* | * Start ourselves | ||
* XXX: To be moved. | |||
*/ | |||
window.foo = new Foo(); | |||
window.foo.start(); | |||
</pre> | </pre> | ||
at the bottom of the file. If the module is initialized elsewhere, e.g. <code>bootstrap.js</code>, you can still safely do that there. | |||
'''Note''' | '''Note''' | ||
* You are required to resolve jshint errors in this stage and remove the file from blacklist. | * You are required to resolve jshint errors in this stage and remove the file from blacklist. | ||
* You are required to have unit tests in this stage. | * You are required to have unit tests in this stage. | ||
* If you register any event listener in start(), '''remember to remove them in stop()'''. Otherwise, our unit tests will get confused because we run all unit tests in the same iframe and module dependencies will mess things up. For the self-initialized script, do call the <code>stop()</code> method on the "global" instance in the test <code>setup()</code>. | |||
=== Stage.2 - Architecture love === | === Stage.2 - Architecture love === | ||
Line 103: | Line 79: | ||
and jsdoc will be generated to 'docs' folder | and jsdoc will be generated to 'docs' folder | ||
=== | === Stage 1 example === | ||
<pre> | <pre> | ||
'use strict'; | 'use strict'; | ||
Line 131: | Line 107: | ||
/** | /** | ||
* DESCRIPTION-OF-START | * DESCRIPTION-OF-WHAT-START-DO | ||
* @memberof Module.prototype | * @memberof Module.prototype | ||
*/ | */ | ||
start: function() { | start: function() { | ||
if (this._started) { | |||
throw 'Module XXX have already started.'; | |||
} | |||
this._started = true; | |||
window.addEventListener('someevent', this); | window.addEventListener('someevent', this); | ||
}, | }, | ||
/** | /** | ||
* DESCRIPTION-OF-STOP | * DESCRIPTION-OF-WHAT-STOP-DO | ||
* @memberof Module.prototype | * @memberof Module.prototype | ||
*/ | */ | ||
stop: function() { | stop: function() { | ||
if (!this._started) { | |||
throw 'Module XXX have not started.'; | |||
} | |||
this._started = false; | |||
window.removeEventListener('someevent', this); | window.removeEventListener('someevent', this); | ||
}, | }, |