array-invoke.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. YUI 3.4.1 (build 4118)
  3. Copyright 2011 Yahoo! Inc. All rights reserved.
  4. Licensed under the BSD License.
  5. http://yuilibrary.com/license/
  6. */
  7. YUI.add('array-invoke', function(Y) {
  8. /**
  9. @module collection
  10. @submodule array-invoke
  11. */
  12. /**
  13. Executes a named method on each item in an array of objects. Items in the array
  14. that do not have a function by that name will be skipped.
  15. @example
  16. Y.Array.invoke(arrayOfDrags, 'plug', Y.Plugin.DDProxy);
  17. @method invoke
  18. @param {Array} items Array of objects supporting the named method.
  19. @param {String} name the name of the method to execute on each item.
  20. @param {Any} [args*] Any number of additional args are passed as parameters to
  21. the execution of the named method.
  22. @return {Array} All return values, indexed according to the item index.
  23. @static
  24. @for Array
  25. **/
  26. Y.Array.invoke = function(items, name) {
  27. var args = Y.Array(arguments, 2, true),
  28. isFunction = Y.Lang.isFunction,
  29. ret = [];
  30. Y.Array.each(Y.Array(items), function(item, i) {
  31. if (isFunction(item[name])) {
  32. ret[i] = item[name].apply(item, args);
  33. }
  34. });
  35. return ret;
  36. };
  37. }, '3.4.1' ,{requires:['yui-base']});