Class: IterableCollection

TrellinatorCore.IterableCollection(obj)

The IterableCollection is used as the primary collection class for returning groups of objects or entities. Whenever you load, for example, an array of Card objects, you will get an IterableCollection back as the return value.

Constructor

new IterableCollection(obj)

Parameters:
Name Type Description
obj Object

an object or Array that you would like to iterate over

Source:
Example
new Trellinator().board("My Board").list("My List")
.cards().each(function(card)
{
    card.postComment("I am card "+card.id());
});

Methods

(static) this.asArray()

Return the object as an Array, preserving keys

Source:

(static) this.each(callback)

Iterate over this collection, passing each element into a callback function

Parameters:
Name Type Description
callback function

the function into which each element will be passed. The first parameter passed in is the element but you can optionally accept a second parameter which is the key

Source:
Examples
notif.board().list("List").cards().each(function(card)
{
    card.postComment("@board Hi! My name is: "+card.name());
});
var params = {name: "Milton",
hobbies: "Listening to music from 9-11 at a reasonable volume"};
new IterableCollection(params).each(function(elem,key)
{
    console.log(key+": "+elem);
});

(static) this.find(comparator)

Return a new IterableCollection containing all objects returned from a callback. The callback can modify the object if required,

Parameters:
Name Type Description
comparator function

a callback function to use in order to identify what objects you're looking for

Source:
Example
try
{
    //using find() instead of transform
    //means we will have cached the list of
    //cards and won't need to reload if we 
    //do another find function later
    notif.board().cards().find(function(elem)
    {
        if(new RegExp("Search.*").test(elem.description()))
            return elem;
        else
            return false;
    }).first().postComment("Twinsies!");
}

catch(e)
{
    Notification.expectException(InvalidDataException,e);
    Trellinator.log("Looks like: "+my_card.name()+" wasn't found");
}

(static) this.findByName(expression)

Convenience function to find an object by name() method, compared with a string or RegExp, or object with a "name" parameter

Parameters:
Name Type Description
expression string | RegExp

the expression to compare to the name of each element to find

Source:

(static) this.first()

Return the first element from this collection

Source:
Throws:

InvalidDataException

(static) this.implode(separator, callback)

Concatenate all the items in a collection in key=value pairs separated by a separator, optionally with each value being augmented by a callback (basically for creating query strings)

Parameters:
Name Type Description
separator string

(optional) the separator to be included between each key=value pair defaults to &

callback function

(optional) a callback to be used to augment the value in each key=value pair

Source:
Example
var params = {name: "Kieth",
              strengths: "Accounts",
              weaknesses: "Eczema"};
HttpApi.call("post",base_url+params.implode("&",function(elem)
{
    return encodeURIComponent(elem);
}));

(static) this.implodeValues(separator, callback)

Return a concatenated string of the values in this collection separated by a common separator, optionally with each value being augmented by a callback, similar to Array.join()

Parameters:
Name Type Description
separator string

(optional) the string to separate each value in the return string, defaults to &

callback function

(optional) a function to augment each value before concatenating

Source:
Examples
//prints "one","two"
console.log('"'+new IterableCollection({one: "one",two: "two"})
.implodeValues(",",function(elem)
{
    return '"'+elem+'"';
})+'"');
//prints a semi-colon separated list of card names
console.log(notif.board().cards().implode(";",function(card)
{
    return card.name();
}));

(static) this.itemAfter(expression, inspector)

Return the item in this collection that appears after a given item, identified by an expression, optionally passing in a callback function used to compare the element with the expression.

By default the expression can be a string or a RegExp and the comparison will be done based on calling the name() method of the element.

A common use case is to find the next list in a Trello board that appears after a list with a given name.

Parameters:
Name Type Description
expression string | RegExp

a string or RegExp indicating the element after which you'd like to find the next item.

inspector function

(optional) a callback used to compare the items to the expression. This should accept 2 parameters:

  • test {string} The expression that you passed in to the original function
  • elem {Object} the item from the collection to be compared to the test expression the function should return true if the element matches the expression.
Source:
Throws:

InvalidDataException

Examples
var next_list = notif.board().lists().itemAfter(new RegExp("Inbox.*"));
var next_card = notif.board().list("Test")
.cards().itemAfter(my_card.id(),function(test,elem)
{
    return test == elem.id();
});

(static) this.itemBefore(expression, inspector)

Return the item in this collection that appears before a given item, identified by an expression, optionally passing in a callback function used to compare the element with the expression.

By default the expression can be a string or a RegExp and the comparison will be done based on calling the name() method of the element.

A common use case is to find the previous list in a Trello board that appears after a list with a given name.

Parameters:
Name Type Description
expression string | RegExp

a string or RegExp indicating the element before which you'd like to find the previous item.

inspector function

(optional) a callback used to compare the items to the expression. This should accept 2 parameters:

  • test {string|RegExp} The expression that you passed in to the original function
  • elem {Object} the item from the collection to be compared to the test expression the function should return true if the element matches the expression.
Source:
Throws:

InvalidDataException

Examples
var prev_list = notif.board().lists().itemBefore(new RegExp("Inbox.*"));
var prev_card = notif.board().list("Test")
.cards().itemBefore(my_card.id(),function(test,elem)
{
    return test == elem.id();
});

(static) this.last()

Return the last element from this collection

Source:
Throws:

InvalidDataException

(static) this.length()

Return the number of items in this collection

Source:

(static) this.random(include)

Return a random element from this collection

Parameters:
Name Type Description
include function

(optional) a callback that returns true if an object should be considered, false if not

Source:
Throws:

InvalidDataException

(static) this.removeRandom(include)

Remove and return a random element from this collection

Parameters:
Name Type Description
include function

(optional) a callback that returns true if an object should be considered, false if not

Source:
Throws:

InvalidDataException

(static) this.reverse()

Reverse the order of this collection

Source:
Throws:

InvalidDataException

(static) this.transform(callback)

Apply a callback to each item in this collection, modifying this collection.

Whatever is returned from the callback will replace the original object, keys are preserved.

If the callback returns false, that key/element pair will be removed.

If you want to find an item in a collection without modifying the collection, use find() instead of transform().

Parameters:
Name Type Description
callback function

a call back that accepts an element of this collection and optionally a second argument which is the key and returns an object to be included in the modified collection, or false and that key/element pair will be removed

Source:
Example
notif.board().cards().transform(function(elem,key))
{
    if((key < 10) && (elem.name() == "ohai"))
        return elem;
    else
        return false;
}).first().postComment("Yep, ohai alright");