[archiving soon] Yahoo! Mojito Framework
Y.mojito.Util.extend
.
Y.mojito.Util.extend
, defined in 'mojito-util', is the equivalent of Y.extend, and can accept
object literals in addition to functions.resourceStore.lazyMojits
is set to true.Below is an example where the ImageResult
controller extends the Result
controller:
mojits/Result/controller.server.js
YUI.add('ResultController', function (Y, NAME) {
Y.namespace('mojito.controllers')[NAME] = {
index: function (ac) {
var result = this.createResultObject(ac);
ac.done({
result: result
});
},
createResultObject: function (ac) {
return {
title: ac.config.get('title'),
text: ac.config.get('text')
};
}
};
}, '0.0.1', {
requires: [
'mojito-config-addon'
]
});
mojits/ImageResult/controller.server.js
YUI.add('ImageResultController', function (Y, NAME) {
var ResultController = Y.mojito.controllers.ResultController,
// Constructor for this controller.
ImageResultController = function () {
// Hook into the original createResultObject method, in order
// to call this controller's augmentResult method.
Y.Do.after(this.augmentResult, this, 'createResultObject')
};
Y.namespace('mojito.controllers')[NAME] = ImageResultController;
// Extend the ResultController, adding the augmentResult custom method.
Y.mojito.Util.extend(ImageResultController, ResultController, {
augmentResult: function () {
Y.Do.currentRetVal.image = {
src: 'myImage'
};
}
});
}, '0.0.1', {
requires: [
'ResultController',
'mojito-util'
]
});
The ImageResult
controller uses Y.mojito.Util.extend
in order to extend the Result
controller and add custom methods. The controller is defined as a function that serves as a constructor. In this function, the controller hooks into createResultObject
in order to call augmentResult
after. Notice that the ImageResult
controller does not have to re-specify the config addon in its requires array since this addon is inferred from the required ResultController
.
Note:
If resourceStore.lazyMojits
is set to true, then mojits that depend on the resources of other mojits must let Mojito know of the dependencies. This ensures that once a mojit is lazy loaded, its dependency mojits are also loaded. Dependencies can be specified in the mojit's defaults.json. Below is how ImageResult's defaults.json would specify that it depends on resources in the Result mojit:
ImageResult/defaults.json
[{
"settings": ["master"],
"dependencies": ["Result"]
}]
Ex. routes.json:
...
"route": {
"verbs": ["get"],
"path": "/path",
"call": "spec.action",
"annotations": {
"client": false
}
}
...
Added the lazyLangs
and lazyMojits
options, which significantly reduce start up time. By default these options are set to false, and can be set to true under the resourceStore
option in application.json. After a resource is lazy loaded, it does not need to be loaded again in subsequent requests.
lazyLangs
and lazyMojits
are ideal options for large applications during development when developers often have to restart an application and don't necessarily use all mojits or languages.
lazyLangs
only loads the lang files necessary for a given request's lang context; this substantially reduces start up time for applications that have many lang files.
Note that lazyLangs
requires lang files to follow proper naming conventions ({mojitName}_{locale}.js or {mojitName}.js for default lang, e.g. Main_es-ES.js or Main.js); default langs are specified with the empty string within the lang file.
lazyMojits
only loads mojits as they appear during a request; this option is especially useful for applications with many mojits that don't appear often.
Note that if lazyMojits
is on then YUI modules within mojits must let YUI know of any YUI modules it requires that are outside of the mojit. Usually this is not necessary since mojits are meant to be self contained, but if it is not, then let YUI know of the external YUI module with this code on top of the requiring YUI module:
if (typeof YUI === 'function') {
// Making sure the PipelineFrame controller is available to this module if lazyMojits is on.
YUI().applyConfig({
modules: {
PipelineFrameMojit: {
fullpath: require('path').join(__dirname, '../PipelineFrame/controller.server.js')
}
}
});
}
This release introduces a set of new APIs and concepts.
Please refer to some of the examples apps under the examples/
folder to get
an overview of what has changed.
Mojito no longer supports index.js
and server.js
to start up the server.
Applications will instead instantiate Mojito as follows:
var libmojito = require('mojito'),
express = require('express'),
app;
app = express();
libmojito.extend(app, { /* context */ });
// at this point, access mojito instance via `app.mojito`
appPort
configuration is no longer supported via application.json
.
Instead, the Express app
instance should call listen()
when ready.
Middleware configuration is no longer supported via application.json
.
Applications can register their middleware using the Express API. To enable
Mojito default list of middleware, use the following:
app.use(libmojito.middleware());
If you want to have more granular control, use the following:
app.use(libmojito.middleware['mojito-handler-static']());
app.use(libmojito.middleware['mojito-parser-body']());
app.use(libmojito.middleware['mojito-parser-cookies']());
app.use(myCustomContextualizerMiddleware());
app.use(libmojito.middleware['mojito-contextualizer']());
app.use(libmojito.middleware['mojito-handler-tunnel']());
app.use(anotherCustomMiddleware());
routes.json
configuration is no longer loaded by default. To tell Mojito to
do so, use the following:
app.mojito.attachRoutes();
Applications can also pass in an array of route configuration names if needed.
ac.url.make()
and Y.mojito.RouteMaker.make()
no longer throws exception.
Instead, the api returns null
in order to provide the application more
control on how best to handle this error.
The ac.url.find()
and Y.mojito.RouteMaker.find()
methods are now
deprecated and will be removed in a future version.
Applications that rely on this API should familiarize with the express-map
package by querying the route object by name
or path
.
Expanded metadata is now removed. This means we will not longer support
synthetic modules that were expanded by default, e.g.:
loader-yui3-base
, loader-yui3-expanded
and loader-app-resolved
.
If you are using any of those 3 entries in the YUI configuration,
you should use loader-app
and loader-app-base
as your seed modules.
In fact we recommend to not customize yui.config.seed
in your application.json
routes.json
:// app.js
app.get('/foo', mojito.dispatch('foo.index'));
app.map('/foo', 'foo');
app.map('/foo', 'get#foo.index');
In addition to setting up the path /foo
to be routed to the Mojito
dispatcher, setup 2 additional "aliases". The second alias is the HTTP method
concatenated with the call
value using the #
delimiter.
This is equivalent to doing this in routes.json
in previous releases.
[{
"settings": [ "master" ],
"foo": {
verbs: [ "get" ],
path: "/foo",
call: "foo.index",
params: { /* optional prams */ }
}
}]
For more detail information, please check any of the applications under
examples/
folder.
ac.config.getRoutes()
now returns the route configuration object using the
format from express-map#getRouteMap()
.express-map
and
express-annotations