Thursday, 20 December 2012

jQuery and Javascript


Here I am adding some memory nudges about javascript and jQuery.


Check for variable is defined


in JavaScript null is an object. There's another value for things that don't exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.
Second, no, they are not directly equivalent. If you really want to check for null, do:
if (null == yourvar) // with casting
if (null === yourvar) // without casting
If you want to check if a variable exist
if (typeof yourvar != 'undefined') // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers
If you know the variable exists but don't know if there's any value stored in it:
if (undefined != yourvar)
if (void 0 != yourvar) // for older browsers
If you want to know if a member exists independent of whether it has been assigned a value or not:
if ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance
If you want to to know whether a variable autocasts to true:
if(variablename)
I probably forgot some method as well...

Taken from: http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript

Wednesday, 12 December 2012

Datepicker date format helper

I have been working on dates and multi language implementation.
I have wrote small class that helps with dates and jQuery.




 
using System.Globalization;
 
namespace HtmlHelpers
{
    public class DateTimeTimeHelper
    {
        /// <summary>
        /// Override jquery date time format with format from current thread.
        /// </summary>
        /// <returns>Javascript format to override jquery date picker settings.</returns>
        public static string OverrideFormat()
        {
            string str = "<script type=\"text/javascript\">";
            str += string.Format("$.datepicker.setDefaults({ dateFormat: '{0}' })", DateTimeTimeHelper.GetDateFormatPatternForCulture());
            str += "</script>";
            str += "</head>";
            return str;
        }
 
        /// <summary>
        /// Get date format pattern for UI culture.
        /// </summary>
        /// <returns>String pattern.</returns>
        public static string GetDateFormatPatternForCulture()
        {
            var dateFormat = GetJavascriptShortDatePattern(System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat);
 
            return dateFormat;
        }
 
        /// <summary>
        /// Gets the javascript short date pattern.
        /// </summary>
        /// <param name="dateTimeFormat">The date time format.</param>
        /// <returns>Reformated short date string.</returns>
        public static string GetJavascriptShortDatePattern(DateTimeFormatInfo dateTimeFormat)
        {
            return dateTimeFormat.ShortDatePattern.Replace("M", "m").Replace("yy", "y");
        }
 
        /// <summary>
        /// Gets the javascript short date pattern.
        /// </summary>
        /// <param name="dateTimeFormat">The date time format.</param>
        /// <returns>Reformated short date string.</returns>
        public string GetJavascriptShortDatePattern(string dateTimeFormat)
        {
            return dateTimeFormat.Replace("M", "m").Replace("yy", "y");
        }
    }
}

Tuesday, 11 December 2012

OOP JS


Running example for me to learn

Running example http://jsfiddle.net/cpoDesign/Cze3X/6/

Creating OOP for js can be done as following.

var masterPage ={
    confirmBoxMsg: 'Selection not available.',  
    settings:{
        version:0.1,        
        gridSettings:{
            width: 'auto',
            height:'auto'
        }
    },
   
    showAlert: function(){    
        alert('test');
    },
    showAlertWithAttr: function(variable){
            alert(variable);
    },
    showAlertWithFromSettings: function(){
        alert('Reading version:'+ this.settings.version);
    },
    dataAccess:function(url,arguments, complete, errorEvent){
        $.ajax({
            url:url,
            type:'GET',
            data:arguments,
            success: complete,          
            error:errorEvent,
        });
    }   
}

// calls logic in printing
//masterPage.showAlert();
    
// logic with wariable
//masterPage.showAlertWithAttr('variable value');

// logic to read variables from object
masterPage.showAlertWithFromSettings()
    
// using ajax call to do some work (not working yet)
    //masterPage.dataAccess("http://www.google.co.uk/",function(res){
    //alert('loaded' + res.responseText);},function(){alert('failed');});
        
   

Sunday, 9 December 2012

How to set up a split-tunnel VPN in Windows

I always after setting up new VPN connection have to remember to do this. I have saved all article that I have found.


By default, when you create a VPN connection, Windows funnels all of the communications from your computer through the VPN. So, if you're logged into a corporate VPN from home to check your email, all of the other Web surfing you're doing on your computer is also being run through your corporate network. This is the default behavior because, from the company's point of view, it's the safest way and ensures all traffic is protected regardless of the destination.
You might not want this behavior, however, for a couple of reasons. First, it allows your company to inspect all of your personal Web traffic while connected to the VPN. Second, it will likely slow down your access to the Web, as everything must first be sent through the VPN.
Split tunneling, on the other hand, configures the VPN connection so that only traffic headed to computers on the corporate network is sent through the VPN connection. Other traffic leaving your computer goes out through your normal network connection.
Follow these steps to set up a VPN connection in Windows Vista that uses split tunneling:
  1. From the Control Panel, choose "Network & Internet."
  2. Click "View Network Status and Tasks."
  3. Click "Manage Network Connections."
  4. Right-click on your VPN connection and select "Properties."
  5. Select the "Networking" tab.
  6. Highlight "Internet Protocol Version 4 (TCP/IP v4)."
  7. Click "Properties."
  8. Click "Advanced."
  9. Uncheck the "Use default gateway on remote network" box.
  10. Click "OK" three times to close the windows you opened.
From that point forward, only traffic destined for your corporate network will be sent through the VPN. All other traffic will use the local network.

Author and original answer

Sunday, 18 November 2012

tfs msbuild MVC3

You know the time spent, when you do change on a web project and want clients to see what you have done?  I have timed one of my releases, with build, publish. It can easily take me up to half an hour, for a bigger project. I have known for a while that you can publish it from TFS build process. So everytime you commit your change into TFS server, you can have automated build triggered and this can do deployment. The company that I work for is doing just this with a website asp.net webforms.

I do not like to copy anyones work, and found out about this process myself and wanted to save myself time when releasing when tfs build succeds.

Step one:

Open visual studio command line:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat""

Now you can run msbuild command.

Step two:
Create your publish.msbuild file.

Step three:

File content
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<PropertyGroup>
        <PublishDestination>C:\Release\Test</PublishDestination>
        <Configuration>Release</Configuration>
    </PropertyGroup>

 <Target Name="Build" >
    <Error Condition="'$(PublishDestination)'==''" Text="The PublishDestination property must be set to the intended publishing destination." />
    <MakeDir Condition="!Exists($(PublishDestination))" Directories="$(PublishDestination)" />
   
    <MSBuild Projects="WebUi\WebUi.csproj"
    Properties="Platform=AnyCPU;Configuration=Release;
    DeployOnBuild=true;    DeployTarget=PipelinePreDeployCopyAllFilesToOneFolder;_PackageTempDir=$(PublishDestination);" />
</Target>
</Project>