Skip to content
This repository has been archived by the owner on Feb 26, 2019. It is now read-only.

Commit

Permalink
added PUSH to protect from prototype override
Browse files Browse the repository at this point in the history
added $.fn.woven to get widget instances. closes troopjs/troopjs#19
  • Loading branch information
mikaelkaron committed Aug 14, 2012
1 parent 963f4fd commit 33cc1ad
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/weave.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define([ "jquery", "troopjs-utils/getargs" ], function WeaveModule($, getargs) {
var FUNCTION = Function;
var ARRAY_PROTO = ARRAY.prototype;
var JOIN = ARRAY_PROTO.join;
var PUSH = ARRAY_PROTO.push;
var POP = ARRAY_PROTO.pop;
var $WHEN = $.when;
var THEN = "then";
Expand Down Expand Up @@ -108,7 +109,7 @@ define([ "jquery", "troopjs-utils/getargs" ], function WeaveModule($, getargs) {
var matches;

// Push dfdWeave on pending to signify we're starting a new task
pending.push(dfdWeave);
PUSH.call(pending, dfdWeave);

$element
// Make sure to remove DATA_WEAVE (so we don't try processing this again)
Expand Down Expand Up @@ -237,7 +238,7 @@ define([ "jquery", "troopjs-utils/getargs" ], function WeaveModule($, getargs) {
var widget;

// Push dfdUnweave on pending to signify we're starting a new task
pending.push(dfdUnweave);
PUSH.call(pending, dfdUnweave);

// Remove WOVEN data
delete $data[WOVEN];
Expand Down Expand Up @@ -277,4 +278,29 @@ define([ "jquery", "troopjs-utils/getargs" ], function WeaveModule($, getargs) {

return $elements;
};

$.fn[WOVEN] = function woven(/* arg, arg */) {
var result = [];
var widgets = arguments.length > 0
? RegExp($.map(arguments, function (widget) {
return "^" + widget + "$";
}).join("|"), "m")
: UNDEFINED;

$(this).each(function elementIterator(index, element) {
if (!$.hasData(element)) {
return;
}

PUSH.apply(result, widgets === UNDEFINED
? $.data(element, WOVEN)
: $.map($.data(element, WOVEN), function (woven) {
return widgets.test(woven.displayName)
? woven
: UNDEFINED;
}));
});

return result;
};
});

0 comments on commit 33cc1ad

Please # to comment.