Tuesday, November 15, 2011

Use C# constants in Javascript

GlobalConstants.cs:
---------------------
namespace Utility
{
    public static class GlobalConstants
    {
        public const string SecurtityKey = "gkjhhkjhykhihfsdjhglkjhgkdfjlghdsfklghfdgeriutyrthvbfkjghdfklghdfskghrtguiutert";
    }
}

InviteUser.chtml:
--------------
<script type="text/javascript" src="@Url.Action("GlobalConstants")"></script>
<script type="text/javascript">
alert(constants.SecurtityKey);
</script>

UserManagementController.cs:
--------------------------------

        public ActionResult GlobalConstants()
        {
            var constants = typeof(BasePage)
                .GetFields()
                .ToDictionary(x => x.Name, x => x.GetValue(null));
            var json = new JavaScriptSerializer().Serialize(constants);
            return JavaScript("var constants = " + json + ";");
        }

Wednesday, September 21, 2011

Get ViewBag value from javascript

<script type="text/javascript">
var roleExist = @Html.Raw(Json.Encode(Model.UserRolePropertyLevel));
var array = @Html.Raw(Json.Encode(@ViewBag.AvailablePPOList));
alert(JSON.stringify(roleExist));
</script>

Wednesday, September 14, 2011

Sorting Array List

http://www.javascriptkit.com/javatutors/arraysort2.shtml

http://www.sitepoint.com/javascript-array-sorting/

Javascript protoType

http://www.codeproject.com/KB/scripting/Observer_Pattern_JS.aspx

function ArrayList() {
            //initialize with an empty array
            this.aList = [];
        }

        ArrayList.prototype =
        {
            Count: function () {
                return this.aList.length;
            },
            Add: function (object) {
                //Object are placed at the end of the array
                return this.aList.push(object);
            },
            GetAt: function (index) //Index must be a number
            {
                if (index > -1 && index < this.aList.length)
                    return this.aList[index];
                else
                    return undefined; //Out of bound array, return undefined
            },
            Clear: function () {
                this.aList = [];
            },
            RemoveAt: function (index) // index must be a number
            {
                var m_count = this.aList.length;

                if (m_count > 0 && index > -1 && index < this.aList.length) {
                    switch (index) {
                        case 0:
                            this.aList.shift();
                            break;
                        case m_count - 1:
                            this.aList.pop();
                            break;
                        default:
                            var head = this.aList.slice(0, index);
                            var tail = this.aList.slice(index + 1);
                            this.aList = head.concat(tail);
                            break;
                    }
                }
            },
            Insert: function (object, index) {
                var m_count = this.aList.length;
                var m_returnValue = -1;

                if (index > -1 && index <= m_count) {
                    switch (index) {
                        case 0:
                            this.aList.unshift(object);
                            m_returnValue = 0;
                            break;
                        case m_count:
                            this.aList.push(object);
                            m_returnValue = m_count;
                            break;
                        default:
                            var head = this.aList.slice(0, index - 1);
                            var tail = this.aList.slice(index);
                            this.aList = this.aList.concat(tail.unshift(object));
                            m_returnValue = index;
                            break;
                    }
                }
                return m_returnValue;
            },
            IndexOf: function (object, startIndex) {
                var m_count = this.aList.length;
                var m_returnValue = -1;

                if (startIndex > -1 && startIndex < m_count) {
                    var i = startIndex;

                    while (i < m_count) {
                        if (this.aList[i] == object) {
                            m_returnValue = i;
                            break;
                        }

                        i++;
                    }
                }
                return m_returnValue;
            },
            LastIndexOf: function (object, startIndex) {
                var m_count = this.aList.length;
                var m_returnValue = -1;

                if (startIndex > -1 && startIndex < m_count) {
                    var i = m_count - 1;

                    while (i >= startIndex) {
                        if (this.aList[i] == object) {
                            m_returnValue = i;
                            break;
                        }

                        i--;
                    }
                }
                return m_returnValue;
            },
            ConvertToJSON: function () {
                return JSON.stringify(this.aList);
            },
            AscSort: function () {
                this.aList.sort(function (firstObject, secondObject) {
                    if (firstObject.PropertyName < secondObject.PropertyName)
                        return -1;
                    if (firstObject.PropertyName > secondObject.PropertyName)
                        return 1;
                    return 0;
                })
            }
        };


function PropertyLevel() {
    this.PropertyId = "";
    this.PropertyName = "";
    this.PropertyValue = [];
}



        var myarrayobject = new ArrayList();
        function CreateRoleProperty(propertyName, propertyId, propertyValueKey, propertyValueText) {
            var objPropertyLevel = new PropertyLevel();
            objPropertyLevel.PropertyId = propertyId;
            objPropertyLevel.PropertyName = propertyName;
            var PropertyValue = new ListEntity();
            PropertyValue.Key = propertyValueKey;
            PropertyValue.Value = propertyValueText;
            objPropertyLevel.PropertyValue[0] = PropertyValue;
            // Add values at the end of the array
            myarrayobject.Add(objPropertyLevel);

           // Ascending Order
          myarrayobject.AscSort();
        }

remove Last Character

myStr = myStr.slice(0, -1)