Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

0.9.7

Compare
Choose a tag to compare
@aljimenez aljimenez released this 09 Aug 06:30
· 15 commits to develop since this release

version 0.9.7

Features

  • Controllers, models, and binders can be defined as a function with a prototype.
  • Support for easily extending YUI modules in a different mojit, by using 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.
  • Controllers inherit the addons of any controller that is listed in its requires array.
  • Mojit dependencies can be specified in defaults.json, which ensures that required dependencies are loaded when 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"]
}]