PageRenderTime 107ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/documentation/appframework/$.proxy.md

https://github.com/maoyao/appframework
Markdown | 59 lines | 36 code | 23 blank | 0 comment | 0 complexity | 37da14395ce75e23cc599b61664b13ca MD5 | raw file
  1. #$.proxy(callback,context);
  2. ```
  3. Creates a proxy function so you can chane "this" context in the function
  4. ```
  5. ##Example
  6. ```
  7. var newObj={foo:bar}
  8. $("#main").bind("click",$.proxy(function(evt){console.log(this)},newObj);
  9. ( $.proxy(function(foo, bar){console.log(this+foo+bar)}, newObj) )("foo", "bar");
  10. ( $.proxy(function(foo, bar){console.log(this+foo+bar)}, newObj, ["foo", "bar"]) )();
  11. ```
  12. ##Parameters
  13. ```
  14. callback Function
  15. Context Object
  16. ```
  17. ##Returns
  18. ```
  19. undefined
  20. ```
  21. ##Detail
  22. $.proxy(callback,context) allows you to create a proxy function that changes the context of "this"
  23. There are times where you want "this" to be something other then the object that the event or function is dispatched on.
  24. ```
  25. var newObj={foo:bar}
  26. $("#main").bind("click",$.proxy(function(evt){console.log(this)},newObj);
  27. or
  28. ( $.proxy(function(foo, bar){console.log(this+foo+bar)}, newObj) )('foo', 'bar');
  29. or
  30. ( $.proxy(function(foo, bar){console.log(this+foo+bar)}, newObj, ['foo', 'bar']) )();
  31. ```
  32. Below we will have an anchor and proxy the click event so "this" is the object {foo:'bar'}
  33. ```
  34. var obj={foo:'bar'}
  35. $("#proxyTest").bind("click",$.proxy(function(){alert(this.foo);},obj));
  36. ```