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

转载于:https://www.cnblogs.com/CodingPerfectWorld/archive/2010/12/03/1895483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值