ASP.NET AJAX Function.createDelegate VS Function.createCallback? [英] ASP.NET AJAX Function.createDelegate vs Function.createCallback?

查看:96
本文介绍了ASP.NET AJAX Function.createDelegate VS Function.createCallback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读这本书曼宁 - asp.net阿贾克斯在行动。页54-58客户代表和回调的描述。

i'm reading the book manning - asp.net ajax in action. on page 54-58 client delegates and callbacks are described.

在短:

客户端代理您可以修改 这个 在事件处理程序本身,以反映另一个对象变量。 (因此它不会触发对DOM事件的事件的DOM对象)

with the client delegate you can change the this variable in the event handler itself to reflect another object. (so it won't be the DOM object that triggered the event for DOM events)

回调你可以传递一个 上下文对象 以事件处理程序,将在事件可用处理程序在第二个参数/参数

既可以通过对象的事件处理程序(无论是这个上下文对象 ),将不会提供的事件处理程序(在它的范围内)。

with both you can "pass" an object to the event handler (be it this or the context object) that wouldn't be available the event handler (in the scope of it).

什么是这些客户代表与回调之间的不同?

What is the different between these client delegates vs. callbacks?

要我看来:

只有对象您想在事件处理程序是可用的以不同的方式访问

only the object you want to be available in the event handler is accessible in different ways:


  • 客户委托:这个

  • 回调:第二个参数

......也许,只有在回调 DomEvent(第一个参数) 而不是客户端代理?!??

...and maybe that only the callback has the DomEvent(first parameter) , but not the client delegate?!??

这是正确的?是这样吗?

Is this correct? Is that all?

也许你能解释一下客户代表和回调之间的区别。

Maybe you can explain the difference between client delegates and callbacks.

推荐答案

我真的不知道这个,但我认为回调是当页面被提交到服务器发生了什么。

I'm not really sure about this but I think the callback is what happens when the page is submitted to the server.

客户端创建委托在那里创建一个委托调用JavaScript方法。

The client create delegate is there to create a delegate for calling JavaScript methods.

该Function.CreateDelegate()方法是真正有用的。

The Function.CreateDelegate() method is really helpful.

比如说你要处理在JavaScript中一个按钮单击事件。 此对象功能可处理该按钮的点击是被点击的按钮。

Say you want to handle a button click event in JavaScript. The "this" object available in function that handles the button click is the button that was clicked.

现在说你有捕捉按钮点击事件,并做了一个JavaScript对象。通常,这指的是JavaScript对象本身,而是因为对象的方法来处理按钮单击事件本实际上是指对引发事件的对象:按钮

Now say you have a JavaScript Object that captures the button click event and does something. Normally "this" refers to the JavaScript Object itself but since Object's method is used to handle the button click event "this" actually refers to the object that raised the event: the button.

要解决这个JavaScript的<一个href=\"https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FReference/Global%5FObjects/Function/apply\"相对=nofollow>创建应用方法。因此,而不是通过在的attachEvent / attachEventListener方法指定JavaScript对象的方法(或$ addHandler操作()方法中的Ajax.NET框架)捕获按钮点击事件,你实际使用apply()方法与这些的attachEvent / attachEventListener。这种变化的背景下,这样这指的是JavaScript对象,而不是引发该事件的对象。

To get around this the JavaScript apply method was created. So instead of capturing the button click event by specifying the JavaScript Object's method in the attachEvent/attachEventListener method (or the $addHandler() method in the Ajax.NET Framework), you'd actually use the apply() method with these attachEvent/attachEventListener. This changes the context so that "this" refers to the JavaScript Object instead of the Object that raised the event.

现在,Function.CreateDelegate()方法,并不仅仅是使用创建客户端代理的适用方法。

Now, the Function.CreateDelegate() method does more than just use the apply method in creating a client delegate.

看看在code:

Function.createDelegate = function Function$createDelegate(instance, method) {
    /// <summary locid="M:J#Function.createDelegate" />
    /// <param name="instance" mayBeNull="true"></param>
    /// <param name="method" type="Function"></param>
    /// <returns type="Function"></returns>
    var e = Function._validateParams(arguments, [
    {name: "instance", mayBeNull: true},
    {name: "method", type: Function}
    ]);
    if (e) throw e;
    return function() {
      return method.apply(instance, arguments);
    }
}

有一件事你应该了解的apply()方法。当你执行它,它执行的是委托方法。为了解决这个问题的方法createDelegate方法做以上。

There's one thing you should know about the apply() method. When you execute it, it executes the method that is the delegate. To get around this the createDelegate method does the above.

所以,我真的不知道回调如何适应这一点,但使用createDelegate方法()方法是当你的JavaScript对象处理由一些UI元素引起的事件是一个好主意。

So, I'm not really sure how a callback fits into this, but using the createDelegate() method is a good idea when your JavaScript Objects handle events that are caused by some UI element.

我假定回调是当请求从浏览器向服务器发送所发生的操作。大部分时间的__doPostBack方法用于preform回调。这个方法需要两个参数...导致回发/回调至服务器的对象,并为回发/回调的参数。这将是客户端代理,虽然完全不同的一个什么东西。

I'm assuming that the callback is the action that takes place when the request is sent from the browser to the server. Most of the time the __doPostback method is used to preform the callback. This method takes 2 parameters...the object that caused the postback/callback to the server, and the arguments for the postback/callback. This would be a something completely different for the client delegate though.

快乐编码

-Frinny

这篇关于ASP.NET AJAX Function.createDelegate VS Function.createCallback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆