Mobil Mewah

Salah satu sumber Inspirasi.

Mobil Sport terbaik

Anda pasti bisa memilikinya.

Bermain dengan pesawat

Salah satu ide yang gila, Balapan di udara.

Bermain di angkasa

Apakah ini salah satu Goals dalam hidup anda? anda pasti bisa mencapainya

Wednesday, 31 July 2013

Form Validation in AngularJS

Every Web application will need a sort of validation before user enter submit button. This usually makes a round trip travel of the data to the server.
For example user fill form, then submit the form to the server. After checking in server, there were errors and the form sent back to the user with error message for the user to fix.

This give an unnecessary round trip request to the application server. In AngularJS, we minimize the roundtrip request to the server, because AngularJS tend to be Ajax application with REST api backend.
We validate the form input from the user before submitted to the server. But remember, we cannot depends on client side validation, which is easily to bypass. We just provide a better user experience in the application by giving error message before user even submit the data to the server. This also ease the burden in the server to process invalid input.

AngularJS provide a great way to validate the form. It is easier to do a form validation with interactive user experience using javascript. You have done the validation in client side using javascript plugin right? Many type of plugin i have used, but i am not bother to know how it works, just include the plugin and do what it specified.

With AngularJS we can do better and more interactive. So now move on to the code.
Let say we have a form :

  <form id="ArticleForm" class="form-horizontal" novalidate method="post" name="ArticleForm"  ng-submit="saveArticle()">

    <legend>

      <h2>

        <span data-ng-bind="page.action">

          Article

        </span></h2>

    </legend>

   
 <div class="alert alert-{{page.result}} ng-cloak" 
data-ng-animate="'myFade'" data-ng-show="page.showMsgBox" 
data-ng-cloak>

      <button class="close" data-ng-click="page.showMsgBox=false" type="button"></button>

        {{page.message}}

    </div>

    <div class="control-group">

      <label class="control-label">

        Title

      </label>

      <div class="controls">

        <input id="id" class="" type="hidden" ng-model="article._id" name="id"></input>

       
 <input id="title" class="input-xlarge" type="text" 
ng-model="article.title" placeholder="Title" required name="title" ></input>

        <span ng-show="ArticleForm.title.$valid"><i class="icon-checkmark-3-green"></i></span>

       
 <span class="error" ng-hide="!(ArticleForm.title.$error.required 
&& ArticleForm.title.$dirty)"><i 
class="icon-cancel-3-red"></i>&nbsp;Please enter Article 
Title</span>

      </div>

    </div>

  <div class="control-group">

       <label class="control-label"></label>

       <div class="controls">

        
 <button class="btn btn-success" 
data-ng-disabled="!ArticleForm.$valid" ng-submit="saveArticle()" 
data-ng-bind="page.action" data-ng-hide="noSubmit" type="submit">

           Create Article

         </button>

       </div>

     </div>

</form>


Lets analyze the form :
  1.  Make sure add novalidate directive in form
    This will prevent default HTML5 validation 
  2. Use requred in the input control
  3. To use the validation status, there are 4 status of the input
    $pristine   : The input not have been touch
    $dirty       : The input already modified by user
    $valid       : The input is valid
    $invalid    : The input is invalid
    To access the variable :
    ArticleForm.title.$pristine
    ArticleForm.title.$dirty
    ArticleForm.title.$valid
    ArticleForm.title.$invalid
  4. After user submit, you will need to reuse the form and reset the form validation status. you can just reset the pristine status of the form.
    $scope.ArticleForm.$setPristine();
The $setPristine() function is usefull to reset all the status, it means also reset your form alert. This function can be found after angular 1.1.1 .
Here are the patch AngularJS $setPristine() update  you can see what it do inside AngularJS.

My selft use it to reset the form validation status to a new fresh state as it just loaded.

And also for the bonus the css style for the form :

.error{
color:red;
}
span.ok, span.ko
{ display:none; float: right; font-size:34px; margin-top: -13px;
}
input.ng-pristine{
border:1px solid Gold;
}
input.ng-dirty.ng-valid
{border:1px solid Green; }
input.ng-dirty.ng-invalid { border:1px solid Red; }
input.ng-dirty.ng-valid ~ span.ok { color:green; display:inline; }
input.ng-dirty.ng-invalid ~ span.ko { color:red; display:inline; }


Just remember, you still need the input validation in server side.

Monday, 29 July 2013

VIM Editor for productive programmer

If you are in linux world and do some basic hacks, the command line you will have by default will be Vi .
Vi Screen Shoot
Vi Editor
There are some command line which more extensible and many plugin, called VIM. This editor not just text editor. In first attempt you will see this VIM application is only simple notepad editor in windows. This is the basic VIM without any plugin loaded. You will like VIM later on.

What a programmer want is of course some smart text editor that can help them typing faster for their code and also give many shortcut which are extensible.
Some of them are line numbering, auto completion, syntax highlighting, and many other more, which of course some professional code editor is not cheap.
With VIM, which is more extended than Vi , VIM available to many other Operating System.
Every basic feature VIM have, usually available in Vi .

My self use VIM not long ago, and start to learn it to make my coding activity even smarter and productive.
And there are millions of VIM plugin out there which can make your life easier as a programmer.
VIM Editor

One of the plugin manager I use with VIM is pathogen. It is easier to use plugin with pathogen, just drop the plugin to the folder, and it will auto load the plugin when VIM start.

I had been using Edit+, Notepad++ , and this time VIM, and I really like VIM for the job. Even you can have your own auto completion in make you type faster than ever. Try it with standard HTML snippet.

So what do you think about VIM , I like also to hear your opinion. Drop me your comment.

Next I will blog about the tools I found most productive to use. Happy VIM !

Tooltip on Twitter Bootstrap

Just now I managed to use Tooltip on Twitter Bootstrap theme for my website. I follow just the template in the example

    <a href="#" rel="tooltip" title="first tooltip">hover over me</a>

But nothing happend as expected. All the file included there, the bootstrap.js and the bootstrap.css with complete feature in it.
Check inside the bootstrap.css, there are entry for tooltip class. Also in the bootstrap.js there are handler for Tooltip.

Searching around via google, i found out that Bootstrap ToolTip feature cannot be used just like that, you need to add some invocation in the head, when document.ready function.

Here are the script to make tooltip activated :

<script type='text/javascript'>
     $(document).ready(function () {
     if ($("[rel=tooltip]").length) {
     $("[rel=tooltip]").tooltip();
     }
   });
</script>

And that's it, the tooltip now active and as what expected. Why this is not stated in the example. Documentation really need to be updated. I documented this one in this blog as my own preference and if you ever found it, surely it will help you.

Sunday, 28 July 2013

How to use syntax Highlighter in blogger

If you are a Developer and do blogging and post your code inside your blog, you want to have it display nicely and also it will help others see your post with smiling face.

There are some utility for this, called syntax highlighter. We can use it in blogger, just insert some javascript in your HTML Head in your blogger template. The code base on JavaScript and almost every blog I found use it. Here are the code to add :





The main file is the shCore.css , shCore.js

Then in your code just add <pre class="brush: js"></pre>

And make sure the javascript is called :





If you also want to contribut to the development or just want to see what happening inside the code just go to SyntaxHighlighter GitHub 

The Brush type supported are here

Improve web application loading

So you have a web application, and it contains HTML in it. The size for each page around 300kb or some have 800kb .Whatever the size your application server generate dynamically, it needs time to load, consume network bandwidth in server, memory usage in server, also consume resource in routers, firewalls, client modem, client CPU and memory and so on.

You can say for client is not to big. They have cache locally , every web browser now have local cache. But still the browser need to check with the web server if there are any change in the content before load it from the cache. It still consume resource.

Now shift to the server point of view, the Web Server and application server. Even a little size squeeze in the HTML output will give a significant resource savings in every part of the infrastructure which process it.
Let say 300Kb HTML page loading for 1 request. What if there were 100 connection, 10k connection. The number become large enough and can become serious bill in the end of the month.

Some developer say bandwidth is cheap, CPU is cheap, but i don't agree with this after reading a book "Release IT" . I remember the CPU cost is cheap told in a Framework community in CakePHP for the excuse of slow performance framework. That not said the PHP language slowliness compared to other language.

So what can we do to improve our web application loading. What i have done is remove all white space in the HTML. All the unneeded white space, tabs, new line, can give significant improvement in size delivered by your server.
Only from white space and new line also tabs, which is common in HTML page to be used and created by Developer and designer, from 200Kb trimmed into 150Kb size. 50Kb is a lot if you serving 10k connections from your web servers.

So what is the tools out there to be use, here are some of my tools i found be useful :
  1. Use Htmlmin from Grunt package
    What is Grunt, it will be another topics to blog. It ease the work of you as developer. Smart developer are lazy for sure. In Grunt there were many package for this type of work. One is Htmlmin
  2. Use Cssmin from Grunt package
    Besides trimming HTML file, you can also trimming CSS files in your web application.
  3. Activate Gzip compression in your WebServer
    In NGINX you can activate the Gzip module so it will sent a compressed file to the browser, and most modern browser support this. More bandwidth to save and make your web application loading faster
  4. Remove Hidden White space image
    Many web designer use image for a white space, use &nbsp which is more small bytes to use.
Even the bandwidth and CPU is more cheaper, there is no excuse to not optimize every bit of the infrastructure. Bigger file in web application means bigger memory to load the application, more CPU cycles in processing, more time in user waiting the application to load, and waste more money.

So try to make improvement in every aspect of the application. It worth a lot even your application only have small user, but will serve better when your site get slashdotted.

Saturday, 27 July 2013

Angular JS for Single Page Application

Recently I came up to see a front end framework , which many of them in the net you can find.

The reason for that is i need a framework to build a web application with ajax technology for front end and also backend with ease.

Then i find many of the framework and learn some of them. I found BackboneJS is interesting, but many coding with MVC framework type coding.
Then i also hear AngularJS which developed by google developer there, obviously by many smart people.
AngularJS also provide MVC standart, but they said in their documentation that AngularJS is MV* type of framework.

And after reading many resources in the web and a Book, i prefer the AngularJS to build my next generation Web Application with Ajax Powered feature.

This is the new journey to developing a single page application, after been with php and django framework. But still the Django design patern experience, i love the django way of design thing.

Next will be the learning to build a single web application using AngularJS.

Twitter Delicious Facebook Digg Stumbleupon Favorites More