项目作者: cedmax

项目描述 :
🌌 a small decoupled, event-driven architectural framework
高级语言: JavaScript
项目地址: git://github.com/cedmax/akase.git
创建时间: 2013-02-28T12:44:13Z
项目社区:https://github.com/cedmax/akase

开源协议:

下载


ākāśe Build Status

ākāśe (sanskrit for “in the sky”/“to the sky”) is a small decoupled, event-driven architecture framework.
It is based on Nicholas Zakas Scalable Javascript Application Architecture and RequireJS and AMD.

Concepts

The concepts of the framework are well represented by the two presentations linked above.
Basically everything gets sandboxed and everyone is happy. (play with it)

Modules & Sandbox

The modules you will have to create are proper AMD modules with this skeleton

  1. define(function(){
  2. 'use strict';
  3. return function(sandbox){
  4. //the logic of the module
  5. function doSomething(){
  6. //do something
  7. }
  8. return {
  9. init:function(config){
  10. //the initialization code
  11. sandbox.subscribe('myEventName', doSomething)
  12. },
  13. destroy: function(){
  14. //optional destroy method, useful to remove callbacks from DOM event
  15. }
  16. };
  17. };
  18. });

As in the example, every module has access to the sandbox, which is supposed to be the only external api accessible, but no one forces you not to require a framework (watch out that you are coupling your code with that specific framework, just saying)

  1. define(['jQuery'], function($){
  2. 'use strict';
  3. return function(sandbox){
  4. function doSomething(){
  5. //do something
  6. }
  7. return {
  8. init:function(config){
  9. $('#myElm').on('click', doSomething);
  10. }
  11. };
  12. };
  13. });

The sandbox API should be defined/extended by you, the only API available out of the box allows to:

  • access the module name

    1. sandbox.module
  • publish an event through the whole architecture

    1. sandbox.publish(eventName, payload)

    Parameters:

    • eventName String - the name of the event
    • payload Object - the optional payload to be sent to the subscribing modules

  • subscribe to an event

    1. sandbox.subscribe(eventName(s), callback)

    Parameters:

    • eventName(s) String|Array[String] - the event(s) the module will subscribe to
    • callback Function - the callback to be invoked (the payload will be injected as argument)

  • namespace your own Api

    1. sandbox.api

Setup & Core

Everything gets started in a proper RequireJS way

  1. <script data-main="main.js" src="/assets/javascripts/require.js"></script>

The main file should require ākāśe core lib in order to take advantage of the framework

  1. require(['akase'], function(core) {
  2. //[...]
  3. });

the core exposes 3 methods in order to:

  • load and initialize a module

    1. start(moduleId, options)

    Parameters:

    • moduleId String - the name of the module
    • options Object - 2 properties allowed:
      • config Object - a configuration object to be injected in the init of the module
      • event String - an event that drives the module start
      • waitForConfig Boolean - (added in v1.1.0) if true the module would wait until a global object akase.config.moduleName is available and will extend the inlined config with it. Useful if you need dynamic data from the page (example below).

  • stop and undefine the module (next start will reload the resource)

    1. stop(moduleId)

    Parameters:

    • moduleId (String) - the name of the module

  • broadcast events into the architecture, it works as the sandbox.publish

    1. notify(event, payload)

    Parameters:

    • eventName String - the name of the event
    • payload Object - the optional payload to be sent to the subscribing modules

example of a proper main.js

  1. require(['akase', 'module1', 'module2', 'module3', 'module4'], function(core) {
  2. var audio = document.createElement("audio"),
  3. canPlayMP3 = (typeof audio.canPlayType === "function" && audio.canPlayType("audio/mpeg") !== "");
  4. core.start("module1", {
  5. config: {
  6. hasMp3Support: canPlayMP3
  7. }
  8. });
  9. core.start("module2");
  10. core.start("module3", { event: "audio:stop" });
  11. });

Wait for config (added in v1.1.0)

  1. core.start("module4", { waitForConfig: true})

In this case the core would wait for a global configuration on the global akase object before starting module4.
Very useful if you need to pass dynamic data from the page rendered by the server to the application.

  1. akase.config['module4'] = {
  2. productId: 'AGS1241S'
  3. }

The core is re-checking for 5 seconds and then give up, no error thrown.




In order to have RequireJS proper loading modules you’d read RequireJS documentation to configure the paths

Thanks

To all the guys that helped me creating ākāśe with their inspiration or making me copy their ideas: Marco Pracucci, Rocco Zanni, Luca Lischetti, Rocco Curcio