JooseX.Meta.Lazy - A trait to make your metaclasses lazy
Role("Some.Lazy.Role", {
trait : JooseX.Meta.Lazy,
has : {
...
},
methods : {
...
}
})
Class("Some.Lazy.Class", {
trait : JooseX.Meta.Lazy,
does : Some.Lazy.Role
has : {
...
},
methods : {
...
}
})
// your class and role will be actually created only in this call
var instance = new Some.Lazy.Class({
})
// alternative declaration - no traits required
LazyRole("Some.Lazy.Role", {
...
})
LazyClass("Some.Lazy.Class", {
...
})
From npm
:
> [sudo] npm install joosex-meta-lazy
In NodeJS (it will export JooseX.Meta.Lazy
to global scope):
require('joosex-meta-lazy')
In browsers (assuming you've completed the 3.1 item from this document):
<script type="text/javascript" src="/jsan/Task/Joose/Core.js"></script>
<script type="text/javascript" src="/jsan/JooseX/Meta/Lazy.js"></script>
JooseX.Meta.Lazy
is a meta-role, making your class (or role) lazy. "Lazy" here means, that the creation of the
metaclass instance will be deferred until the first instantiation of class (or first consumption of role).
If you have a big class hierarchy (like the bridged ExtJS hierarchy for example or your own framework), this module can reduce the initialization time.
You can apply it as a trait (the 1st example in the synopsys), or you can use the new declaration helpers: LazyClass
or LazyRole
Note, that by default, subclasses uses the same metaclass as their parents. Thus, if you'll subclass the lazy class, the resulting class will be also lazy. If you'd like to avoid this, see the next section for details.
If you are using the lazy metaclass and still wants to create a normal class, you can use new builder eager
for that:
Class("Some.Lazy.Class", {
trait : JooseX.Meta.Lazy,
...
})
Class("Some.Eager.Class", {
isa : Some.Lazy.Class // will provide the lazy meta for this class
eager : true // will create class immediately
})
This extension is supported via github issues tracker: http://github.com/SamuraiJack/JooseX-Meta-Lazy/issues
You can also ask questions at IRC channel : #joose
Or the mailing list: http://groups.google.com/group/joose-js
Web page of this module: http://github.com/SamuraiJack/JooseX-Meta-Lazy/
General documentation for Joose: http://joose.github.com/Joose
All complex software has bugs lurking in it, and this module is no exception.
Please report any bugs through the web interface at http://github.com/SamuraiJack/JooseX-Meta-Lazy/issues
Many thanks to Malte Ubl for the idea of this module.
Nickolay Platonov nplatonov@cpan.org
Copyright (c) 2009, Nickolay Platonov
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of Nickolay Platonov nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.