Archive for April, 2010

Jquery Rich Array Documentation

If you want to use the JQuery Rich Array and you were hoping for some documentation, well its inside the .JS file but I am posting it here for reference purposes

/***************************************************************************
 *   Copyright (C) 2007 by Vladimir Kadalashvili                                        *
 *   Vladimir.Kadalashvili@gmail.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
/*
A simple jQuery plugin for varios manipulations with arrays
*/ 

jQuery.richArray = {

    /*
        Checks whether an array contains some value
	@param {Array} array - an array in which we search for a value
	@param {Mixed} value - the value we search for
	@return {boolean} true if the array contains the value, otherwise false
    */
    
    in: function(array, value) {
    },
    
    /*
        Produces the duplicate-free version of the array
	@param {Array} array
	@returns {array}  - an array without duplicates
    */
    unique: function(array) {
    
    },
    
    /*
        Finds the difference between two arrays.
	@param {Array} array1 
	@param {Array} array2
	@return {Array} array of values which are present in the first array, but not in the second
    */
    
    diff: function(array1, array2) {
    
    },
    
    /*
        Finds the intersection of two arrays
	@param {Array} array1
	@param {Array} array2
	@return {Array} - the array of values wich are present in both arrays
    */
    
    intersect: function(array1, array2) {
    
    },
    
    /*
        Applies filter to the array, using callback function
	@param {Array} array - an array which we apply filter to
	@param {Function} fn - the filter function. If it returns the value that may be evaluated as TRUE, the value will be included to the returned array.
	@param {Object} scope - the scope of the callback function. Default is jQuery.richArray
	@returns {Array} - an array of values for which callback function returned true 
    */
    
    filter: function(array, fn, scope) {
    
    },
    
    /*
        Applies callback function for each element in the input array, and returns array of values that this function returned
	@param {Array} array - an array which we should apply callback to
	@param {Function} fn - callback function
	@param scope - the scope of the callback function. Default is jQuery.richArray
    */
    map: function(array, fn, scope) {
    },
    
    /*
        Computes the sum of all array elements.
	@param {Array} array - an array we should compute the sum for
	@param {Mixed} init - the initial value of the sum. Default is 0.
	@returns {Mixed} the sum of all elements of the input array
    */
    
    sum: function(array, init) {
    },
    
    /*
        Calculates the production of all elements of the array
	@param array - an array we should compute production for
	@param init - the initial value. Default is 1.
	@returns {Mixed} - the production of all elements of the input array
    */
    
    product: function(array, init) {
    },
    
    /*
        Reduces the array. One-elemen arrays are turned into their unique element, others are retured untouched
	Examples: 
	jQuery.richArray.reduce([3]) -> 3
	jQuery.richArray.reduce([3, 5]) -> [3, 5]
    */
    
    reduce: function(array) {
    },
    
    /*
        Creates new version of array without null/undefined values
	@param {Array} array - input array
	@returns {Array} - an array without null/undefined values
    */
    
    compact: function(array) {
    },
    
    /*
       Creates a new version of the array that doesn't contain the specified value
       @patam {Array} array - input array
       @param {Mixed} value - the value that shouldn't be included to the returned array
       @returns {Array} - a new version of the input array without specified value
    */
    
    without: function(array, value) {
    },
    
    /*
        If the passed argument is an array, returns it untouched, otherwise returns an empty array.
	For internal use.
    */
    
    getArray: function(array) {
    },
    
    /*
        if the passed argument is a function, returns it untouched, otherwise returns an empty function
   */
    
    getFunction: function(fn) {
        if (!(fn instanceof Function)) fn = new Function();
	return fn;
    }    
    
    
}; 

Homepage: Jquery Rich Array Plugin

Serialize and Deserialize to JSON from ASP.NET

Its very easy to serialize an object to .NET
Simply create some object, normally a custom class with some attributes.
Normally you have a list of these and you want to serialize to JSON to use it from client side code.

If you do the following

var s = new System.Web.Script.Serialization.JavaScriptSerializer();
string resultJs = s.Serialize(result);

you will end up with a JSON array that you were looking for:

[ 
  { "Desc" : "Corn 100 ",
    "ItemNo" : "123456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.21
  } ,
  { "Desc" : "Ice 100 ",
    "ItemNo" : "323456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.50
  } ,
  { "Desc" : "Meat 100 ",
    "ItemNo" : "423456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.11
  } 
  
]

However if you try to deserialize this back into an object by running

var json = new System.Web.Script.Serialization.JavaScriptSerializer();
var result = json.Deserialize<ShoppingCartItem[]>(jsonItemArray);
    

You will get a NULL type Exception.

The solution is to create a simple resolver as follows

    using System;
    using System.Web;
    using System.Web.Compilation;
    using System.Web.Script.Serialization;
    
    namespace XYZ.Util
    {
        /// <summary>
        /// as __type is missing ,we need to add this
        /// </summary>
        public class ManualResolver : SimpleTypeResolver
        {
            public ManualResolver() { }
            public override Type ResolveType(string id)
            {
                return System.Web.Compilation.BuildManager.GetType(id, false);
            }
        }
    }
    

2) Use it to serialize

    var s = new System.Web.Script.Serialization.JavaScriptSerializer(new XYZ.Util.ManualResolver());
    string resultJs = s.Serialize(result);
    lblJs.Text = string.Format("<script>var resultObj = {0};</script>", resultJs);
    

You will get something like the following this time (note new __type included this time)

[ 
  { 
    "__type":"XYZ.Data.Entities.ShoppingCartItem, XYZ.Data, Version=1.7.1.0, Culture=neutral, PublicKeyToken=null",
    "Desc" : "Corn 100 ",
    "ItemNo" : "123456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.21
  } ,
  { 
    "__type":"XYZ.Data.Entities.ShoppingCartItem, XYZ.Data, Version=1.7.1.0, Culture=neutral, PublicKeyToken=null",
    "Desc" : "Ice 100 ",
    "ItemNo" : "323456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.50
  } ,
  { 
    "__type":"XYZ.Data.Entities.ShoppingCartItem, XYZ.Data, Version=1.7.1.0, Culture=neutral, PublicKeyToken=null",  
    "Desc" : "Meat 100 ",
    "ItemNo" : "423456",
    "OriginalQty" : 50,
    "Qty" : 50,
    "UnitPrice" : 10.11
  } 
  
]

3) Use it to deserialize

    System.Web.Script.Serialization.JavaScriptSerializer(new XYZ.Util.ManualResolver());
    var result = json.Deserialize<ShoppingCartItem[]>(jsonItemArray);
    

References: Comment by Manuel Abadia on ASP.NET AJAX Extensions Internals – Web Service Proxy Generation

How to completely disable ViewState and ControlState

Here is a code snippet that will COMPLETELY disable ViewState and ControlState.

Please note, if you want to disable viewstate, you can set “EnableViewState” to false for the page, however you will still see “VIEWSTATE” in the page. The reason for that is because the hidden ViewState HTML field also contains “Control State”, which is used by server controls (either built in controls or custom 3rd party controls) and is not disable-able (for core functionality that their control depends on).

However you can still disable this, with disastrous consequences on postback.

USE THIS only if you have NO POSTBACKS whatsoever and you are living in a happy client side world of javascript and web service calls.

Put this following code inside your ASP.NET page

private class DummyPageStatePersister  :PageStatePersister
{
    public DummyPageStatePersister(Page p) : base(p) { }
    public override void Load() { }
    public override void Save() { }
}

private DummyPageStatePersister _PageStatePersister;
protected override PageStatePersister PageStatePersister
{
    get
    {
        if (_PageStatePersister == null)
            _PageStatePersister = new DummyPageStatePersister(this);
        return _PageStatePersister;
    }
}

Please note this will has disastrous consequences if you attempt to do a postback because you have essentially killed the control state.

Optimization WordPress Plugins & Solutions by W3 EDGE