zoukankan      html  css  js  c++  java
  • ASP.NET AJAX $get vs. $find

    icrosoft ASP.NET AJAX comes with a new method for getting a reference to an object representing an element on the page, e.g. input control, button, etc. -- $get.

     

    However, you might notice that there is another function that appears to do same thing – $find…  So, what’s the difference between them, and which should you use and when?

     

     

    First, let’s see how the two methods are implemented…

     

    $get is an alias for getElementById.  In addition, this function has additional code for those browsers that have not implemented getElementById.   Here is how it’s defined:

     

    var $get = Sys.UI.DomElement.getElementById = function Sys$UI$DomElement$getElementById(id, element) {

        /// <param name="id" type="String"></param>

        /// <param name="element" domElement="true" optional="true" mayBeNull="true"></param>

        /// <returns domElement="true" mayBeNull="true"></returns>

        var e = Function._validateParams(arguments, [

            {name: "id", type: String},

            {name: "element", mayBeNull: true, domElement: true, optional: true}

        ]);

        if (e) throw e;

        if (!element) return document.getElementById(id);

        if (element.getElementById) return element.getElementById(id);

        // Implementation for browsers that don't have getElementById on elements:

        var nodeQueue = [];

        var childNodes = element.childNodes;

        for (var i = 0; i < childNodes.length; i++) {

            var node = childNodes[i];

            if (node.nodeType == 1) {

                nodeQueue[nodeQueue.length] = node;

            }

        }

        while (nodeQueue.length) {

            node = nodeQueue.shift();

            if (node.id == id) {

                return node;

            }

            childNodes = node.childNodes;

            for (i = 0; i < childNodes.length; i++) {

                node = childNodes[i];

                if (node.nodeType == 1) {

                    nodeQueue[nodeQueue.length] = node;

                }

            }

        }

        return null;

    }

     

    $find is a shortcut for Sys.Application.findComponent.   Components on the client… in JavaScript?  Yes!

     

    Here are some characteristics of components that differentiated them from controls and behaviors (source:  http://ajax.asp.net/docs/tutorials/CreatingCustomClientComponentsTutorial.aspx):

     

    1.  Components typically have no physical UI representation, such as a timer component that raises events at intervals but is not visible on the page.

    2.  Have no associated DOM elements.

    3.  Encapsulate client code that is intended to be reusable across applications.

    4.  Derive from the Component base class.

     

     

    So, the Sys.Application.findComponent function (implementation of the $find alias) is defined as follows:

     

    var $find = Sys.Application.findComponent;

    function Sys$_Application$findComponent(id, parent) {

            /// <param name="id" type="String"></param>

            /// <param name="parent" optional="true" mayBeNull="true"></param>

            /// <returns type="Sys.Component" mayBeNull="true"></returns>

            var e = Function._validateParams(arguments, [

                {name: "id", type: String},

                {name: "parent", mayBeNull: true, optional: true}

            ]);

            if (e) throw e;

            // Need to reference the application singleton directly beause the $find alias

            // points to the instance function without context. The 'this' pointer won't work here.

            return (parent ?

                ((Sys.IContainer.isInstanceOfType(parent)) ?

                    parent.findComponent(id) :

                    parent[id] || null) :

                Sys.Application._components[id] || null);

        }

     

     

     

    It is recommended that you use the findComponent (or $find) method to get a reference to a Component object that has been registered with the application through the addComponent (or $create) method.

     

    Note: if parent is not specified, the search is limited to top-level components; if parent represents a Component object, the search is limited to children of the specified component; if parent is a DOM element, the search is limited to children components of the specified element.

     

     

    Looking at the code, you might be wondering on the performance difference…  My tests show that as a general rule (i.e. given a page of average complexity and average number of controls and components) the performance is roughly the same between the $get and $find.  However, my tests were not comprehensive. 

     

     

    Bottom line:

    1. Both, $get and $find, appear return a reference to a DOM object created using markup or at runtime
    2. Both, $get and $find, appear to return a component reference
    3. While both methods work on elements and components, it’s recommended to use $get for elements and $find for components.

    from:

    http://blogs.msdn.com/b/irenak/archive/2007/02/19/sysk-290-asp-net-ajax-get-vs-find.aspx

  • 相关阅读:
    bootstrap1
    vim格式化代码实际上就是 "缩进代码", 命令是等号=
    thinkphp如何一次性的上传多个文件,在文件域中可以多选?
    linux下, 再次遇到使用thinkphp的模板标签时,报错used undefined function ThinkTemplatesimplexml_load_string() 是因为没有安装 php-xml包
    再谈 Mysql解决中文乱码
    碳膜电阻+1N5408二极管?
    华为发布业界首套物联网网络建设方法论
    页面错误提示
    Linux下Redis服务器安装配置
    svn 强制用户添加注释 和 允许用户修改注释
  • 原文地址:https://www.cnblogs.com/CodingPerfectWorld/p/1895483.html
Copyright © 2011-2022 走看看