Skip to content

Instantly share code, notes, and snippets.

@pbedat
Created March 9, 2014 20:03
Show Gist options
  • Select an option

  • Save pbedat/9453755 to your computer and use it in GitHub Desktop.

Select an option

Save pbedat/9453755 to your computer and use it in GitHub Desktop.
SelectableCollection.js
angular.module("xAmine").factory("SelectableCollection", function($rootScope){
function SelectableCollection(items, eventNamespace){
eventNamespace = eventNamespace || "selectable-collection";
var my = this;
my.Items = items;
my.Selected = null;
var fire = function(ev, arg){
$rootScope.$broadcast(eventNamespace + ev, arg);
};
my.Select = function(item){
my.Selected = item;
};
my.Add = function(item){
my.Items.push(item);
fire(":added", item);
};
my.Remove = function(){
if(!my.Selected)
return false;
var removeAt = my.Items.indexOf(my.Selected);
my.Items.splice(removeAt, 1);
var removed = my.Selected;
removeAt = removeAt == my.Items.length ? my.Items.length - 1 : removeAt;
my.Select(my.Items[Math.max(0, removeAt)]);
fire(":removed", removed);
return removed;
};
}
return SelectableCollection;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment