Показать сообщение отдельно
  #1022 (permalink)  
Старый 19.12.2013, 12:33
Профессор
Посмотреть профиль Найти все сообщения от Maxmaxmaximus6
 
Регистрация: 19.12.2013
Сообщений: 180

Вот короче готовое ядро, оцените код? Лучше стало?

var ui = window['ui'] = {

    name       : 'ui',
    children   : {},
    filters    : {},
    services   : {},
    directives : {},
    animations : {},
    controllers: {},



    init: function(element) {

        var $compile = this.service('$compile');
        var $scope = this.service('$scope');

        $compile(element)($scope);
    },



    module: function(name) {

        var children = this.children;
        var module = children[name];

        if (!module) {
            module = Object.create(this);

            module.name = name;
            module.children = {};
            module.filters = Object.create(this.filters);
            module.services = Object.create(this.services);
            module.animations = Object.create(this.animations);
            module.directives = Object.create(this.directives);
            module.controllers = Object.create(this.controllers);

            children[name] = module;
        }

        return module;
    },



    directive: function(name, factory) {

        if (factory) {
            return this.resource('directives', name, factory);
        }

        var directives = this['directives'];
        var resource = directives[name];

        if (!resource) {
            return null;
        }

        var instance = resource.instance;

        if (!instance) {
            factory = resource.factory;
            instance = this.invoke(factory);

            if (isFunction(instance)) {
                instance = {
                    link: instance
                };
            }

            if (!instance.name) {
                instance.name = name;
            }

            if (!instance.priority) {
                instance.priority = 0;
            }

            if (!instance.restrict) {
                instance.restrict = 'A';
            }

            resource.instance = instance;
        }

        return instance;
    },



    service: function(name, factory) {

        return this.resource('services', name, factory);
    },



    filter: function(name, factory) {

        return this.resource('filters', name, factory);
    },



    animation: function(name, factory) {

        return this.resource('animations', name, factory);
    },



    controller: function(name, controller) {

        var controllers = this.controllers;
        var $parseController = this.service('$parseController');

        if (controller) {
            controllers[name] = $parseController(controller);
            return this;
        }

        controller = controllers[name];

        if (!controller) {
            controller = $parseController(window[name]);
        }

        return controller;
    },



    invoke: function(func, context, locals) {

        if (!locals) {
            locals = {}
        }

        if (isArray(func)) {
            var inject = func.slice();

            func = inject.pop();
            func['$inject'] = inject;
        }

        if (!func['$inject']) {
            var params = func.toString().match(/\(([\s\S]*?)\)/)[1].match(/[\w$]+/img);
            func['$inject'] = params || [];
        }

        locals['$module'] = this;

        var services = func['$inject'].map(function(injectName) {

            return locals[injectName] || this.service(injectName);

        }.bind(this));

        return func.apply(context, services);
    },



    instantiate: function(func, locals) {

        var instance = Object.create(func.prototype);
        var returns = this.invoke(func, instance, locals);

        if (isObject(returns)) {
            instance = returns;
        }

        return instance;
    },



    resource: function(type, name, factory) {

        if (factory) {
            var resource = {
                factory : factory,
                instance: null
            };

            this[type][name] = resource;
            return this;
        }

        resource = this[type][name];

        if (!resource) {
            return null;
        }

        var instance = resource.instance;

        if (!instance) {
            factory = resource.factory;
            instance = this.invoke(factory);
            resource.instance = instance;
        }

        return instance;
    }

};
Ответить с цитированием