2/18/2017

The comma operator in Javascript

Comma operator in Javascript can be used to evaluate expressions from left to right and return the result of last statement.

(stament1,statement2,statement3) 

The statement above means that

  • Execute statement1
  • Execute statement2
  • Execute statement3
  • Return result of statement3


The following example illustrates the concept:

var a=1;
var b = (a++,a++,a++,a);  

b is 4, which means the 3 statements has been executed and last statement is returned.

When you combine comma operator with ternary operator, the number of lines can be reduced:

(isStringValidated?(processString(),showMessage()) : showError())

The example above checks if the string is validated, if it is validated 2 methods are called from left to write. If it is not validated, then another method is called.


Rerefences

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
[2] http://stackoverflow.com/questions/9579546/when-is-the-comma-operator-useful



Type Coercion in Javascript

What is type coercion: Converting a value from one type to another is often called "type casting," when done explicitly, and "coercion" when done implicitly (forced by the rules of how a value is used).[1]

In order to code less and do more, one can benefit from type coercion. I will explain this with 2 examples.

Example 1

Return error, if there is no error return warning. Otherwise return empty string

Solution 1

function showMessage(errorMsg,warningMsg){
     var result="";
     if(errorMsg) result= errorMsg;
     else result = warningMsg;
     return result;
}

Solution 2 with ternary operators

function showMessage(errorMsg,warningMsg){
     return errorMsg ? errorMsg : (warningMsg ? warningMessage: '')
}

Solution 3 using type coercion

function showMessage(errorMsg,warningMsg){
     return errorMsg || warningMsg || '';
}
In the third solution, logical OR operator indicates that if first sentence is not true, then return second sentence.

When the example is getting complex, you can observe the advantage more easily.

Example 2

If the element is dirty and if there is an error than return error,if element is dirty and there is no error and there is a warning return the warning. Otherwise return empty string.

Solution 1

function showMessage(isDirty,errorMsg,warningMsg){
     var result="";
     if(isDirty){
          if(errorMsg) result= errorMsg;
          else result = warningMsg;
     }
     return result;
}

Solution 2 with ternary operators

function showMessage(isDirty,errorMsg,warningMsg){
     return isDirty? (errorMsg ? errorMsg : (warningMsg ? warningMessage: '')):''
}

Solution 3 using type coercion

function showMessage(isDirty, errorMsg,warningMsg){
     return (isDirty && (errorMsg || warningMsg)) || '';
}
In the third solution, logical AND operator indicates that if first or second statement is false, then return false, if both or them are true, then return the second statement.

The following table shows all combinations of the logical AND and OR operators in javascript.


a= true b =true a=true b =false a=false b=true a=false b=false
a AND b b b a a
a OR b a a b b

Please note that, "X= false" means the following statement returns true: "X == false" The following list shows the X values that result in false

  • null
  • undefined
  • false
  • Empty String
  • 0
  • []

References

[1] You Don't Know JS: ES6 & Beyond Chapter 4



10/12/2015

The huge benefits of Ionic Framework in hybrid applications

First of all, Ionic Framework is free. That means a lot if you are a startup company or if you want to develop something with a small budget.

Ionic is fully integrated with AngularJS. Instead of finding workarounds to integrate your UI framework with Angular, Ionic supports it from the beginning.

You can easily use Cordova with Ionic for all device specific support. If you are familiar with Cordava, you can easily understand how Ionic works with Cordova especially for deployment of packages. Adding Cordova to the existing application can be time consuming and manual process but with Ionic you don't need to worry about this. Ionic can also be used with PhoneGap or trigger.io

In hybrid mobile application development, the most time-consuming part is testing and deployment. Ionic View feature allows developers to preview their application repository instantly using their native application in iOS or Android. The viewer also provides versioning of application releases. I saw that similar approach has been adopted by SalesForce Mobile development, and for me this should be the way all mobile UI frameworks should start supporting.
http://view.ionic.io/

Ionic has its own market where you can share your plugins/themes. Since Ionic is modular, it is very easy to download and integrate plugins with your application. There is also a section for starters, it contains project source files and documents.
https://market.ionic.io/
There is creator functionality in beta release which allows user to develop Ionic UI using drag-drop function. When you start creating the application,it is automatically added to your application repository.

There is also a playground functionality like JSFiddle, where you can start development. You can preview quickly your final result.


8/06/2015

Showing All Current T-SQL Executions in the database

We would like to see if there exists a query running/suspended in the database without using SQL Activity Monitor. The query also works in SQL Azure.

In order to do that, we used the following query:

SELECT r.total_elapsed_time,text, task_state
FROM
   sys.dm_exec_sessions s
   LEFT  JOIN sys.dm_exec_connections c
        ON  s.session_id = c.session_id
   LEFT JOIN sys.dm_db_task_space_usage tsu
        ON  tsu.session_id = s.session_id
   LEFT JOIN sys.dm_os_tasks t
        ON  t.session_id = tsu.session_id
        AND t.request_id = tsu.request_id
   LEFT JOIN sys.dm_exec_requests r
        ON  r.session_id = tsu.session_id
        AND r.request_id = tsu.request_id
   OUTER APPLY sys.dm_exec_sql_text(r.sql_handle) TSQL
   where r.total_elapsed_time >1

I only select relevant columns for me and an elapsed time filter.

Ref:
http://stackoverflow.com/questions/7503456/nhibernate-to-linq-3-2-generate-error-sql-on-operator


4/16/2014

Remote Web Deployment to Azure VM

Visual Studio Environment offers different types of web-site publish. One of them is Web Deployment. This option allows user to directly publish application to a server IIS remotely. However there are steps to be followed before using this feature:

  1. Install web deploy in the Azure VM. You need to choose full installation, the deployment related service will automatically be started( or you can run the command net start msdepsvc) Link http://technet.microsoft.com/en-us/library/dd569059(v=WS.10).aspx
  2. Default port for web deploy is 8172. You can either choose to use it or define your own port. You can set this port via IIS> Click Server> Management Service ( you need to stop service to configure)
  3. Add endpoint using Azure Portal. Click Virtual Machine and select your machine. Click endpoints and add new TCP endpoint, both public and private ports should be 8172
  4. Right click your project and click publish. Choose Web Deploy. In your server settings, server name should be your virtual machine path with 8172 port. 

4/09/2014

Using Knockout-Kendo Bindings with RequireJS

There is a project called Knockout-Kendo bindings in github which provides binds for each kendo widget and allows to use only knockout bindings.
https://github.com/kendo-labs/knockout-kendo/tree/master/src
The project is AMD supported but you need to be careful when giving aliases to your paths. It is important to know that the project is dependent on jquery,knockout and kendo.
I assume you are using web version of Kendo.
If you want to include it to your project using RequireJS, the following configuration can be used in your in main js file:
I imported all of vendor specific filed into my lib folder.
requirejs.config({
    paths: {
        'knockout': '../lib/knockout/knockout-2.3.0',
        'jquery': '../lib/jquery/jquery-1.9.1',
        'kendo': "../lib/kendo/js/kendo.web.min",
        "kendobindings": "../lib/knockout/knockout-kendo.min"
    },
    shim: {
        'jquery': {
            exports: '$'
        },
        "kendo": {
            deps: ["jquery"]
        },
        "kendobindings": {
            deps: ["jquery", "kendo", "knockout"]
        }
    }
});



3/03/2014

How to change VM image region (affinity group) in Microsoft Azure

Today, we needed to create a new virtual machine using an existing image in a different region that we worked so far. Due to the fact that images are region specific,we followed the steps above to create the VM:

  • Find the blob URL using Azure Portal. In VM section, there exists three tabs. One of them is images.Click this tab and find your image. Then look at image blob URL( i will call it source blob URL)
  • Create a storage and container in the region that you want to use image if you didnt do. I will call this container as destination base URL
  • Install Microsoft Azure Storage tools. It will be used to copy blob vhd item from one container to another. In current Azure Portal, there is no option to copy blob items. You can also do this by downloading the  vhd and upload it again manually.
azure account import {file path for publishsettings} (you can download publishsettings via shellscript,see next step)
azure vm disk upload {source url} {destination url} {destination storage password}

  •  Install Microsoft Azure ShellScript. It will be used to register your blob as an image.After you execute the command,operating system will be asked, write operating system that the image contains. For my case it was "windows".
 add-azureaccount 
get-azurepublishsettingsfile 
import-azurepublishsettingsfile {file path for downloaded publishsettings}
add-azureVMImage -ImageName {image name} -MediaLocation {destination url} -Label {label of image }