Saturday, August 27, 2011

Javascript - Executing callback methods with prototype methods and JQuery Ajax requests

If you have something like this:
var data = {};

var test = MyClass();

test.CommonMethod(ajaxSuccess,data);



function ajaxSuccess(data){

alert("ajax done!");

}

And this doesn't work because most probably your class is something like this:
function MyClass()

{

this.message = null;

this.callbackFunction = null;

}



MyClass.prototype.CommonMethod = function(callback, data) {

this.callbackFunction = callback;

$.ajax({

url: "http://mydomain.com/executetest/",

dataType: 'jsonp',

data: data,

success: CommonSuccessMethod

});

}



MyClass.prototype.CommonSuccessMethod = function(data) {

this.callbackFunction(data);

}



The reason why this doesn't work, the "this" on the "CommonSuccessMethod" is out of scope (from ajax request). To make it work, you must include the reference of the object executing the ajax request.

Here's a modified version of the class:

function MyClass()

{

this.message = null;

this.callbackFunction = null;

}



MyClass.prototype.CommonMethod = function(callback, data) {

this.callbackFunction = callback;

$.ajax({

url: "http://mydomain.com/executetest/",

dataType: 'jsonp',

data: data,

success: CommonSuccessMethod,

classInstance: this

});

}



MyClass.prototype.CommonSuccessMethod = function(data) {

this.classInstance.callbackFunction(data);

}




Now, on the method "CommonSuccessMethod". you can access the executing class thru "this.classInstance" and this will execute your "ajaxSuccess" method.

Friday, August 26, 2011

Javascript - How to create a class


function MyClass()

{

this.publicProperty = "This is public";

var privateProperty = "This is private";

}



MyClass.prototype.CommonMethod = function() {

//this works

return this.publicProperty;



//this doesn't work

return this.privateProperty;

//neither this

return privateProperty;

}


Calling the private variables from the prototype methods are not allowed since they are not defined on the scope of the constructor, hence, don't have access to the constructor's private variables.

The beauty of prototype methods is that only a single instance of the method is created and shared by all instantiated objects of that type.

In order to access the private variables on the prototype methods, have this instead on your class:

function MyClass()

{

this.publicProperty = "This is public";

var privateProperty = "This is private";

this.GetPrivateProperty = function () { return privateProperty; };

}


Since the method "GetPrivateProperty" was defined within the scope of the class, then it has access to the private variables. The methods defined within the class have a separate instance on the memory unlike the prototype methods. They are not shared to all instance of that class type.

So, a good programming practice would be to define prototype methods. :)

Sunday, August 7, 2011

C# - generic type properties with arguments on initialization

Abstract Base Class

public abstract class CADWebServiceBase<T> where T : class
{
    protected dynamic CustomHeader;
    abstract protected String WebServiceName { get; } 

    protected T _service;
    protected T Service
    {
        get
        {
            if (this._service == null)
                this._service = (T)Activator.CreateInstance(typeof(T), this.WebServiceName);
            return this._service;
        }
        set
        {
            this._service = value;
        }
    }
}

Child Class

public class CADGeographyWebService : CADWebServiceBase<CADGeographyServiceSoapClient>
{
    protected override string WebServiceName
    {
        get { return CADWebServices.SOAP_Geography; }
    }

    public String HelloWorld()
    {
        String results = this.Service.HelloWord();
          
        return results;
    }

}

Thursday, August 4, 2011

C# - Instantiate generic type parameters on class functions

protected T GetUserHeader<T>() where T : new()
{
    if (this.CustomHeader == null)
    {
        T userheader = new T();

        PropertyInfo appProperty = userheader.GetType().GetProperty("myProperty");
        if (appProperty.CanWrite)
             appProperty.SetValue(userheader, this.UserConfig.Application, null);
        this.CustomHeader = userheader;
    }
    return (T)CustomHeader;
}

Wednesday, August 3, 2011

MSSQL - SPLIT String and get occurence position

CREATE FUNCTION [dbo].[SPLIT_GET]

(

@inputString NVARCHAR(max),

@delimiter CHAR,

@findposition INT

)

RETURNS NVARCHAR(MAX)

AS

BEGIN



DECLARE @startposition INT

DECLARE @endposition INT

DECLARE @inputLength INT

DECLARE @lengthToParse INT

DECLARE @currentposition INT

DECLARE @resultString NVARCHAR(MAX)



SET @currentposition = 0

SET @startposition = 1

SET @endposition = 1

SET @inputLength = LEN(@inputString)

SET @resultString = null



WHILE @startposition <= @inputLength 

BEGIN

SET @currentposition += 1

SELECT @endposition = charindex(@delimiter,@inputString,@startposition)

IF @endposition <> 0

BEGIN

SET @endposition = @endposition - 1

END

ELSE

BEGIN

SET @endposition = @inputLength

END



SET @lengthToParse = @endposition - @startposition + 1

IF @currentposition = @findposition

BEGIN

SET @resultString = substring(@inputString,@startposition,@lengthToParse)

BREAK

END



SET @startposition = @endposition + 2



END

RETURN @resultString



END
Sample usage: SELECT dbo.SPLIT_GET('mark.libres','.','2')
Will return "libres"