0.2.1 • Published 5 years ago

micro-namespace v0.2.1

Weekly downloads
667
License
SEE LICENSE IN LI...
Repository
bitbucket
Last release
5 years ago

micro-namespace


Wrap libraries into a namespace.

clone

repository

$ hg clone https://bitbucket.org/frostbane/micro-namespace

npm

$ npm install micro-namespace

usage

AMD require

var ns = require("micro-namespace");

include script

<script type="text/javascript"
        src="micro-namespace.js"></script>

examples

simple namespace

// create fb.common.util namespace
namespace("fb.common.util");

// add methods to fb.common.util
fb.common.util.formatDate = function(...){ ... };
fb.common.util.logError   = function(...){ ... };

// use the methods/classes
if(!fb.common.util.formatDate("2017/12/14")){
    fb.common.util.logError("format failed");
}

namespace and object

// create fb.common.extra namespace with methods
namespace("fb.common.extra", {
    getDateNow   :function(){ ... },
    getServerUrl :function(){ ... },
    AjaxParser   :function(){ ... },
});

// use the methods/classes
var now = fb.common.extra.getDateNow();

var host = fb.common.extra.getServerUrl();

var ax = new fb.common.extra.AjaxParser();

amd require

var ns = require("micro-namespace");

ns("fb.ajax", {
    getUsername :function(){ ... },
});

var user = fb.ajax.getUsername();

initialize object

the third parameter (init) is called under the context of the second parameter (obj) after the namespace is created.

namespace("fb.common.upload", {
    uploadFile   :function(){},
    initUploader :function(){},
    resetErrors  :function(){},
}, function(){
    // exported methods can be accessed with this
    this.initUploader();
    
    this.resetErrors();
    
    $(function(){
        $("#cal").datepicker();
    });
});
0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.22

7 years ago

0.0.1

7 years ago