define(["lib/underscore", "./id_generator", "./binding"],
function(_, IDGenerator, Binding) {
"use strict";
/**
* Creates a new BindingList
*
* @class Manages a list of items. When an item is added, a Binding is
* returned that can be used to later remove the item from the
* list.
*
* @name BindingList
* @private
*/
var BindingList = function() {
/** @alias BindingList.prototype */
var self = {};
var list = {};
/**
* Adds an item to the BindingList and returns a Binding.
*
* @param {Object} obj The object to add to the list
* @return {Binding} A binding that, when cleared, will remove the item
* from the list.
*/
self.add = function(obj) {
var id = IDGenerator.uuid();
list[id] = obj;
return Binding(function() {
delete list[id];
}, window);
};
/**
* Clears the BindingList
*/
self.clear = function() {
list = {};
};
/**
* Returns the array of items in this BindingList
*/
self.items = function() {
return _.values(list);
};
/**
* Iterates over each item
*
* @param {Function} fn Function to invoke with each item.
*/
self.each = function(fn) {
_.each(list, fn);
};
/**
* Invokes every member of this list that is a function. Arguments
* passed to this method are passed on to the functions.
*/
self.callAll = function() {
var args = arguments;
_.each(list, function(obj) {
if(_.isFunction(obj)) {
obj.apply(window, args);
}
});
};
return self;
};
return BindingList;
});