Monday, November 26, 2012

Automatically map XmlhttpRequest post data to MVC action partameters

After converting a html Form to a xmlHttpRequest post request, the MVC action method can no longer get the mapped parameter value sent from client. The post data string is the exact between the two cases.

It turns out it is caused by not setting the Content-Type header in xmlhttprequest  object, and it is reasonable that server action method will not try to map the parameter value from post data, as with post request, the post data may be binary, json or any other formats.

So in order to fix the issue, the only required change is setting the content type explicitly for the request:

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

And the server action method just stars to work again.

Monday, November 5, 2012

Install iso file on Windows 7 with WinRAR

The Virtual CD application provided by Microsoft no longer supports Windows 7, so it cannot by used to install iso file. If you do not want to burn the iso file into a real DVD, or using third party application to mount  the iso file to a virtual driver, you can just unzipped the iso file using WinRAR, which is a well known safe application to install on Windows 7.
After it is unzipped, just start the setup file (setup.exe), it should work as if it is installed from a DVD. Certainly, this will not work if you need to boot the box from the iso file.

Sunday, November 4, 2012

MS SQL Server 2008 sa authentication failure

Just installed a sql server 2008 and try to login using sa account. The login failed and few issues have to be checked using Windows authentication account before making sa account work:

1. Login SQL server management studio with windows account, right click server property menu, select Security, and check "SQL Server and Windows Authentication mode" under Server authentication section.

2 Still in server management studio, select Security folder, select sa user and right click property, check status item, and be sure it is Granted and Enabled in settings page


Monday, October 15, 2012

When do you need to send AutoRelease message to an object

If the object is created with convenience method, like [NSMutableArray arrayWithCapacity], then the convenience constructor already sends the AutoRelease message to the object when it is created, so when the auto release pool or block is ended, it will send the release message to the object.

If the object is created with the alloc or copy constructor, and the object is returned to caller as a method return value or output parameter, then you should call AutoRelease message on the object. The reason is the receiver does not create the object, so he is not the own of the object and is not responsible to release the object. While after the object is returned by the method, then the creator of the object lose the control of the object and cannot actively release the object. The only limited control the original creator has on the object is sending autorelease message to it before relinquishing the control of the object, and that will be sure the object gets released sometime when the autorelease pool is closed.

However, in both cases, if the autoreleased object is created on a long-living auto release pool, then the object will last for long time and that will cause the object dose not be released quickly.

Sunday, October 14, 2012

javascript function scope sample

Javascript scope is used to enclose the internal logic and only expose the necessary public information to external. Basically, within a object or function scope, only properties or function  defined on the object are visible to external as public info through the object. Any local variables defined with var, or inner function are only available to internal logic.

var globalFunction = function () {    // global, can be accessed anywhere.
    alert('hello world');
}

var containerFunction = function () { // globale namespce, can be accessed anywhere.
   
    var subFunction = function () {    // private namespace method
        alert("I'm Private");
        globalFunction();              // We can access global Function here 2 levels deep.
    }
   
    containerFunction.test = function () {  //public namespace method
        alert("test");
    }

    callableFunc = function () {   //global method, because variable is global
        alert("callable");
    }

    function localFunc(){   //only visible to local
       alert("local");
    }

    varible ="8";  //unlike function, new varibles without var gets added to global window object

    globalFunction();                 // We can access global Function here 1 level deep.
    subFunction();                     // We can access subFunction inside containerFunction.
    localFunc();
    alert(varible);
}

containerFunction();
containerFunction.test();
callableFunc();
alert(varible);

//failure cases
//test();  //test is not visible in global scope
//subFunction(); //namespace private method and only exists inside containerFunction
//containerFunction.callableFunc(); //callableFunc is not belonging to namespace, it is global
//localFunc() //not visible

Note, unlike 

javascript self invoking function parameter

The sample javascript self invoking function is defined as follows
(function(parmeter1){
    alert(parameter1);
}("hello world");

Notice the data in the last parentices is the actual caller that will invoke the function, which passes the argument's value as "hello world".
Inside the function definition, the parameter1 is replaced by the string "hello world", and shows the alert.

Wednesday, October 10, 2012

addSubView and pushViewController difference

For ios project, addSubView and PushViewController can be used to display a new view. However, they are quite different.

addSubView is a instance method of View class, and it adds a view item into itself. If the subview occupies the whole screen, then it has the same effect of display a new view.

pushViewController is a instance method if UINavigationViewController, so it can only be used on UINavigationViewController object, it has a better isolation than previous method, as it does not add the new view as a subview in the parent view.

Tuesday, October 9, 2012

Array returned from jQuery selector and html element

When using jquery id selector, it will only return a single object. However, similar to other jquery selector, the returned object is wrapped in a jquery object array. Note all jquery selector methods return an array, as it is always possible to match more than one elements for the selector.
If you call a jquey method to retrieve the attribute on the returned array, like val(), it actually returns the attribute of the first element. When the object is selected by id, it is perfectly fine, as there is only one element. However, if the array contains multiple elements, it does not have much sense to just return the first element's value when calling the method on the array. Although, in both cases, setting the value is fine, as it makes sense to set the same value for all elements or a single element.
In order to return a particular jquery element from a multiple element array, call jQuery('selector:eq(index)') method. Remember, the returned value from the method is still wrapped in a jquery array, the only difference is the array will only contain a single jquery object.
In order to return a particular htmlElement, call jQuery('selector').get(index). The returned object is a single html object, (not a array, not a jquery object), and it has all the applicable attribute defined for html element such as value attribute.



Monday, September 24, 2012

Nil and NSNull and NSDictionary

Nil and Null cannot be added into NS collection such as NSDictionary as a value. That is why when checking whether a key exists or not in a dictionary, the following code will work

if ([mydict objectForKey:@"mykey"]) {
    // key exists.
}

However, what about if we really want to add the value for a key that is nil? In this case NSNull is what we want. NSNull can be added in the NSDictionary and also retrieved to represent a nil value.

Sunday, July 29, 2012

jquery constructor with context $(selector,this)

Inside the jquery event handler, quite often we need to select a sub element within the passed in event element, and do some operation on it. The passed in event element is accessible from the this object. That is the place you will see a lot of $("selector", this) get called.

$(selector, this) is another jquery constructor, the second parameter takes a context variable. In this case, instead of selecting the elements from the current document, it just selects the matched elements from the context parameter. In the event handler, "this" represents the DOM object that the current event happens on it, to use the jquery method on it, we need first converts it to jquery object with $(this).

Wednesday, July 18, 2012

Common jquery shortcut

$(...)
=>
jQuery()

All jquery methods are started by calling jQuery(), which can be written as $(...)
 


$(function(){...});
=> 
$(document).ready(function(){...});
The function will be called when document is ready.
Do not mix this shortcut with javascript self-invoked anonymous function, the ending () makes the js function to be invoked, and thbe leading () indicates this is not normal function.
(function(){
...
})();
 

$.fn.myFeature = function () { ... } 
=>
jQuery.prototype.myFeature = function () { ... } 
 
The function to create a jquery plugin by adding method into p


Sunday, May 27, 2012

MVC for asp.net web form developer

For asp.net web form deveoper, when starting work with MVC, you may need to know few key points.

1. MVC does not have the rich UI controls available on web form
Reading any MVC books, you will be supprised none of them talking much about ui controls. So how can you put the treeview, or gridview control to your web page? The answer is MVC is an open framework, it is created to to incooperate the new web technology easily, like HTML5, jquery, etc, so it cannot define the ui control's mark up as web form does. So by design, it depends you to select and use any ui markup you get, and for now the answer is jquery ui, or other third party jquery ui extension.

2. MVC does not associate request/response to a backend individual.
Unlike web form, each client request or response is assocated to a backend aspx file, MVC lets you associate a backend method in controller class to each client request and response, this allows to incooperate the partial page render, and other ajax features easily. This is much better than web form, and it is key improment in MVC.

3. MVC dependency: javascript, jquery, CSS, ajax
Unlike what you heard from most MS experts, or MVC books, MVC does not depend on lambada expression, Linq or Entity Framework. In my opinion, you better avoid those topic when first learning MVC, as they have nothing to do with the key points of MVC. The really dependency of MVC are jquery, CSS and ajax. As they are the necessary elements to make a good MVC project.

In addition, I did not see much value in using Entity Framework, or Workflow Framework in your project, it allow you do certain things more easy, but on the other hand, you lose the fine control of the project due to those generated code. It has the same issue as the generated typed Dataset in ADO.NET. I would rather to spend a little more time and know exactly which line of code is doing what, than save some time but having a bunch of files doing things magically without I know the details. There are too many genius in Microsoft, but not enough commonsense.

Thursday, May 3, 2012

xib, view and viewcontroller

It is a little confusing how xib, view and viewcontroller works together in ios application. When app starts, it creates an instance of viewcontroller class, viewcontroller class asks to load xib file by calling initnibwithname mtheod, this will load the xib file into memory. At this moment, the owner of the xib file is viewController instance. It is more clear if this is done with [NSBundle loadNibNamed] method. The next step is mapping the outlet from xib resource objects to the viewcontroller instance's IBOutlet, usually this involves passes a view object to viewcontroller's view property. Once this is done, it totally depends on viewcontroller to decide how to display its view property on screen. So basically, what xib file does is creating all resource objects and associating them to the specified IBOutlet variable, and leave the owner to use it. Note the instance asks to load the xib must be the type specified in xib's file owner type, so that the IBOutlet settings can be matched between the real owner and xib file. Jonathan

Monday, April 2, 2012

jquery mobile and jquery version mismatch problem

In order for jquery mobile javascript works properly, the version of jquery library used in the page must match the jquery mobile's version. For example, the current version 1.0.1 of jquery mobile requirs the jquery version of 1.6.2. If the latest jquery version (1.7.2) is used, then it may not work.
A simple example is creating a two-page html file, then clicking the link from the first page to show the second page will not work. Taking half hour to figure this out.

Jonathan

Wednesday, March 21, 2012

Know which one is accessed in objective C: Instance variable or property

It may create some subtle bugs if you do not pay attention on which one is accessed inside a objective c method, as property and instance variable have the same name.
If you access from outside, then you know you are accessing the property as instance variables can not be accessed from outside of the class instance methods.
However if you access the variable or property by name from inside the class, it really depends on whether the key word "self" is used or not.
If you access through self, like self.myVariable, then the property is used and most likely, it is also retained.
If you access through the variable alone, then the instance variable is used and no property related feature, like retain, will apply.
So if next time you ios app crashes, pay attention to which one you are access inside your member method.

Wednesday, February 1, 2012

This pointer and function pointer in javascript

As shown below, Java or c# will pass current "this" pointer from caller to callee implicitly

void caller()
{
callee(); //caller will be invoked on this pointer used to invoke caller
}


In Javascript, when calling callee() inside caller(), if the invoking object is not specified as this.callee(), it does not implicitly pass the current "this" pointer to invoke callee(). And the global Window object will be used as "this" to invoke function callee.

When invoking constructor in javascript, inside the constructor, the "this" pointer is the current class, not the window object.

Another related issue is, in javascript, function defined in one class can be assigned to another object as method, (Note function is a standalone unit of statements and a method is attached to an object and can be referenced by the this keyword), and when invoking the method from another object, its this pointer is the assigned object, and has nothing to do with the class or instance that defines the function.

Sample code:
function bar()
{
alert(this.constructor.name);
}

function somefunc()
{
alert(this.constructor.name);
}

function apple()
{

this.bar = somefunc;
bar(); //call global bar
his.bar(); //call somefunc

this.internalMethod = function() {
alert("internal method this: " + this.constructor.name);
}
}


//to test the above code, calling following method from html page script section
var myApple = new apple();
myApple.internalMethod(); //myApple is this pointer used to invoke the method
var me = myApple.internalMethod;
me(); //window is used as this pointer to invoke the method


Full js file for testing

function Apple() {
try {
alert("constructor: "+ this.constructor.name);
this.color = "red";
this.bar = somefunc;
bar();
this.bar();

this.internalMethod = function() {
alert("internal method this: " + this.constructor.name);
}

this.internalMethod();

this.getInfo();
}
catch (ex) {
alert("exception: " + ex);
}
}

Apple.prototype.getInfo = function() {
alert(this.color + ', type: ' + this.constructor.name);
};

function bar() {

alert("bar method this: "+ this.constructor.name);
}

// anti-pattern! keep reading...
function somefunc() {
this.getInfo();
alert("somefunc this: "+ this.constructor.name);
}

Full html file for testing

var myApple = new Apple();
myApple.internalMethod();
var me = myApple.internalMethod;
me();