代码之家  ›  专栏  ›  技术社区  ›  balupton

jQuery和Mootools的不可知Javascript框架适配器?

  •  2
  • balupton  · 技术社区  · 14 年前

    我将把我的一系列jQuery项目移植到Vanilla Javascript(纯Javascript,没有框架),并想知道是否有任何现有的[框架适配器/框架无关适配器]?

    例如,我想象着这样的事情:

    // My Project
    (function(){
        // Fetch all the elements using Sizzle Selector System
        var $els = Agnostic.find('.penguins');
        $els.hide();
    
        // Perform a Ajax Request
        Agnostic.ajax({
            dataType: 'json',
            sucess: function(){
    
            },
            error: function(){
    
            }
        });
    });
    
    /**
     * Our Agnostic Framework
     * Provides a framework agnostic interface for jQuery and MooTools
     */
    var Agnostic = {
        framework: null,
        Framework: null,
    
        /**
         * Initialise our Agnostic Framework
         */
        init: function(){
            switch ( true ) {
                case Boolean(jQuery||false):
                    Agnostic.Framework = jQuery;
                    Agnostic.framework = 'jQuery';
                    break;
    
                case Boolean(MooTools||false):
                    // Check for Sizzle
                    if ( typeof Sizzle === 'undefined' ) {
                        throw new Error('MooTools interface requires the Sizzle Selector Engine.');
                    }
                    Agnostic.Framework = MooTools;
                    Agnostic.framework = 'MooTools';
                    break;
    
                default:
                    throw new Error('Could not detect a framework.');
                    break;
            }
        }
    
        /**
         * Our Element Object
         * Used to Wrap the Framework's Object to provide an Agnostic API
         */
        Element: {
            /**
             * Create the Element Wrapper
             */
            create: function(Object){
                var El = new Agnostic.Element;
                El.Object = Object;
            },
    
            /**
             * Hide the Element
             */
            hide: function(){
                switch ( Agnostic.framework ) {
                    case 'jQuery':
                        this.Object.hide();
                        break;
    
                    case 'MooTools':
                        this.Object.setStyle('display','none'); 
                        break;
                }
            },
    
            /**
             * Show the Element
             */
            show: function(){
                switch ( Agnostic.framework ) {
                    case 'jQuery':
                        this.Object.show();
                        break;
    
                    case 'MooTools':
                        this.Object.setStyle('display',''); 
                        break;
                }
            }
        },
    
        /**
         * Fetch elements from the DOM using the Sizzle Selector Engine
         */
        find: function(selector){
            var Element = null;
    
            // Fetch
            switch ( Agnostic.framework ) {
                case 'jQuery':
                    Element = jQuery(selector);
                    break;
    
                case 'MooTools':
                    Element = new Elements(new Sizzle(selector)); 
                    break;
            }
    
            // Wrap
            Element = Agnostic.Element.create(Element);
    
            // Return Element
            return Element;
        },
    
        /**
         * Perform an Ajax Request
         * We use the jQuery.ajax interface here
         * But they are more or less the same
         */
        ajax: function(request){
            // Send Request
            switch ( Agnostic.framework ) {
                case 'jQuery':
                    jQuery.ajax(request);
                    break;
    
                case 'MooTools':
                    (new Request(request)).send();
                    break;
            }
    
            // Wrap
            Element = Agnostic.Element.create(Element);
    
            // Return Element
            return Element;
        }
    };
    
    2 回复  |  直到 14 年前
        2
  •  0
  •   Sean Vieira    14 年前

    我相信你会发现 FuseJS 就是你要找的那种东西。

    引用自述的第一行:

    JavaScript框架共享类似的 特性和功能,例如 DOM操作,事件注册, 和CSS选择器引擎。FuseJS公司 将这些框架整合成一个稳定的, 高效、优化的核心

    它也很漂亮,因为它的 sandboxed natives