Return to Snippet

Revision: 57937
at June 15, 2012 20:25 by mikkeldamm


Initial Code
/******************/
/*** List class ***/
/******************/
(function () {

    function List(type) {

        var _type = type.Type;
        var _list = {};
        var _count = 0;

        this.Add = function (obj) {

            /// <summary>Adds an object to the end of the List</summary>

            if (obj.Type === _type) 
            {
                _list[obj.Id] = obj;
                _count++;
            }
            else 
            {
                $.error("Cannot convert source type (" + obj.Type + ") to target type (" + _type + ")");
            }
        };

        this.Get = function (id) {

            /// <summary>Get object by id</summary>
            /// <param name="id">The id of the object wich to be find</param>
            /// <returns type="object">Returns the requested object if exist</returns>

            if (_list[id] != undefined && _list[id] != null)
                return _list[id];

            return null;
        };

        this.GetAt = function (index) {

            /// <summary>Get object by index</summary>
            /// <param name="id">The index of the object wich to be find</param>
            /// <returns type="object">Returns the requested object if exist</returns>

            if (_list[index] != undefined && _list[index] != null)
                return _list[index];

            return null;
        };

        this.GetAll = function (loop) {

            /// <summary>Get objects in collection</summary>
            /// <param name="loop">The function loop the return to</param>
            /// <returns type="object">Returns the collection of objects</returns>

            for (var item in _list) {
                loop(_list[item]);
            }
        };

        this.Remove = function (id) {

            /// <summary>Remove the object with the current id from the List</summary>
            /// <param name="id">The id of the object wich to be removed</param>

            delete _list[id];
            _count--;
        };

        this.RemoveAt = function (index) {

            /// <summary>Remove the object at the index from the list</summary>
            /// <param name="index">The index from the list where the object wich to be removed</param>

            delete _list[index];
            _count--;
        };

        this.Length = function () {

            /// <summary>Get length of the collection</summary>
            /// <returns type="object">Returns the length of the collection</returns>

            return _count;
        };

    }

    window.List = List;

} (window))



/********************/
/*** Module model ***/
/********************/
(function (window) {

    var type = "Module";

    function Module(id) {

        this.Type = type;
        this.Id = id;

    }

    Module.Type = type;

    window.Module = Module;

} (window));

Initial URL

                                

Initial Description
The closest to a generic List in javascript. 
When calling constructor - new List(Module) - add model in constructor parameter

Initial Title
Generic List in Javascript

Initial Tags
list, array

Initial Language
JavaScript