0

i am trying to make a custom module based application myself.

1st the idea:

i want to have couple of modules which all will represent a file )

1 module: Object, with filename. Object.js 2nd module: Utils, with filename Utils.js

now as you can see in my code i am using get script to load all those files and prepare a global app object which i can use all accross my application.

However i want to combine all my js at the end of the development and minify it, so basically i dont know how to check ( and register ) those modules with requiring the file itself. Here are my app.js and my Object.js

app.js:

var shop = (function () {

    'use strict';

    var modules = [];

    var getModulesDir = function () {
        return document.location.origin + "/js/App/modules/"
    }

    var registerDefaultModules = function () {

        modules.push({
            name: "Object",
            alias: "Object"
        }, {
            name: "Utils",
            alias: "Utils"
        });
    };

    var loadModules = function () {

        jQuery.each(modules, function (key, module) {

            if (module.name == "") {
                console.error("loading script without name");
                return true;
            }

            if (module.alias == "") {
                module.alias = module.name;
            }

            if (typeof shop[module.alias] !== "undefined") {
                return true;
            }

            window.moduleAlias = module.alias;

            var path = "";

            if (typeof module.path != "undefined") {
                path = module.path;
            }

            if (path == "") {
                path = getModulesDir() + module.name.split('.').join('/') + ".js";
            }

            $.getScript(path)
                .done(function (script, textStatus) {

                })
                .fail(function (jqxhr, settings, exception) {
                    console.error('failed loading a script')
                });
        });
    }

    var init = function () {

        registerDefaultModules();
        loadModules();
    }

    return {
        init: init
    };

})();

jQuery(document).ready(function () {
    shop.init();
});

Object.js

(function($, app) {
    $(window).load(function(event) {

        'use strict';

        app.Object = {

            getSize: function(object) {

                var size = 0,
                    property;

                for (property in object) {
                    if (object.hasOwnProperty(property)) {
                        size++;
                    }
                }

                return size;
            }
        }

        return app;
    });
})(jQuery, shop);

i could be totally wrong, and if so would be nice if someone show me the right way of doing it.

Upalr
  • 2,140
  • 2
  • 23
  • 33
Izopi4a
  • 490
  • 3
  • 18
  • 1
    StackOverflow is for focused questions, here's what I'm trying to do but I get this error instead, or I get this behavior instead. Right now you are just asking us to write/debug your code for you... Plus, don't try a new module system, use one of the existing module systems out there, commonjs, amd, umd, system – Ruan Mendes Jun 13 '16 at 14:04
  • 1
    is http://requirejs.org/ something for you – lordkain Jun 13 '16 at 14:05
  • In addition, a new module system coupled with jQuery is a very bad idea! – Matías Fidemraizer Jun 13 '16 at 14:07
  • Juan i dont know what kind of error u are getting but this is my copy/paste working code. And no i am not asking you debug my code, i ask how to define my modules in order for them to be eather required by a file, or nto required because they already exist. @lordkain hmm.. you are correct i am stupid enough not to find this lib. Thanks – Izopi4a Jun 13 '16 at 14:13
  • a lot of libs out there. hope it is what youre looking for – lordkain Jun 13 '16 at 14:17
  • This question would be much better focused if you were to include details regarding your build system, or something else that shows how you attempted to complete this task. (the minify process etc. you request) - as it stands you simply attempt to "include" them. – Mark Schultheiss Jun 13 '16 at 20:06
  • http://stackoverflow.com/questions/9287823/combine-and-minify-multiple-css-js-files might get you started – Mark Schultheiss Jun 15 '16 at 13:54

0 Answers0