29 เมษายน 2555

jquery plugin สำหรับใช้ barcode scaner กับ web application HTML form input

วันนี้เจอปัญหาของ web application ตัวนึงซึ่งมี form รับ input เป็น Serial Barcode

ปกติอุปกรณ์ barcode scanner ทั่วไปจะแสกน serial เหมือน keyboard แล้วตามด้วยการกด "Enter" key stroke

ปัญหา 

ใน HTML web form

ถ้าพิมพ์อะไรในช่อง input  นี้แล้วกด Enter จะเป็นการ submit form นั้นทันที ถ้าในฟอร์มเราต้องกรอก input ข้อมูลอื่นด้วยหลายช่อง ทันทีที่แสกนบาร์โค้ดเสร็จจะเหมือนกด submit ทันทีทั้งที่ยังกรอกข้อมูลช่องอื่นไม่ครบ

Solved

แก้ด้วยการเขียน jquery plugin ป้องกัน form submit เวลาที่ยิงสแกน barcode

(function($){
    $.fn.acceptBarcode = function(barcodeClass){
        var canSubmit = false;

        this.submit(function(e){
            //e.preventDefault();
            return canSubmit;
        });

        this.find('input.' + barcodeClass).each(function(){
            console.log('accept barcode for ' + $(this).attr('name'))

            $(this).bind('keyup', function(e){
                var code = (e.keyCode? e.keyCode : e.which);
                if(code == 13){ // Enter key pressed.
                    canSubmit = false;
                    console.log('serial enter detected - disable form.submit()');
                }
            })

            $(this).bind('focus', function(){
                canSubmit = false;
                console.log('serial input focus - disable form.submit()');
            })

            $(this).bind('blur', function(){
                canSubmit = true;
                console.log('serial input blur - enable form.submit()');
            });
        });
    };
})(jQuery);



วิธีใช้งาน..
<input class="barcode" type="text" 
$(document).ready(function(){
    $('form').acceptBarcode('barcode');
});

11 เมษายน 2555

เขียน Python webapp ด้วย Django+Google App Engine+Cloud SQL - part2 deploy

series content :


django settings.py  ติดต่อฐานข้อมูล Cloud SQL


DATABASES = {
   'default': {
       'ENGINE': 'google.appengine.ext.django.backends.rdbms',
       'INSTANCE': 'project-id:instance1',
       'NAME': 'my_database_name',
   }
}

ตั้งค่า INSTANCE และชื่อฐานข้อมูลตามที่สร้างไว้ใน https://code.google.com/apis/console


ตอนนี้เราอยากทดลองติดต่อฐานข้อมูล Cloud ให้ได้ก่อน โดยยังไม่ต้องเขียน models เอง วิธีง่ายสุดเปิดใช้ django.contrib.admin กับ auth (ระบบ login) ที่มากับ django นี่ล่ะ

10 เมษายน 2555

เขียน Python webapp ด้วย Django+Google App Engine+Cloud SQL -part0 สมัครใช้ API

series content :

Update June, 2012

ตอนนี้ Google ประกาศเก็บเงินค่าใช้บริการ Cloud SQL แล้ว
Dear Cloud SQL previewer
As was previously announced, from June 12th 2012 use of the Google Cloud SQL service will be charged.
To continue using your Cloud SQL instances after June 12th 2012, you will need to enable billing for your projects before then by signing in to the Google APIs console and clicking the Billing tab. Once billing is enabled you can choose (or change) the billing plan for your instances from the Cloud SQL tab. Your instances will not be available after June 12th 2012 if you do not enable billing beforehand.
ตั้งแต่วันที่ 12 มิย. 55 ถ้าอยากจะใช้งาน Cloud SQL ต้องเปิด Billing Service มี  Pricing Package คิดตามการใช้งานจริง ถ้าทดลองใช้งานแบบ Per Use Billing Plan จะถูกกว่า

วิธีการสมัครใช้งาน Google Cloud SQL Limited Preview

ตอนนี้ Google เปิดให้ใช้งาน  Cloud SQL  สำหรับ developer ที่สนใจทดสอบเท่านั้น ยังไม่ได้เปิดใช้ 100% ขั้นตอนการสมัครใช้เวลาพอสมควร อธิบายวิธีการสมัครไว้หน่อย

เข้าไปที่หน้า https://code.google.com/apis/console ถ้ายังไม่เคยใช้งานก็ใช้ Gmail Account login

เปิดไปที่แทป Services มองหา Google Cloud SQL



กด Request access.. link ไปที่หน้าฟอร์มสมัครใช้งาน .. (Google Cloud SQL Limited Preview Signup)

9 เมษายน 2555

เขียน Python webapp ด้วย Django+Google App Engine+Cloud SQL - part1 การติดตั้ง SDK

series content :


ก่อนหน้านี้เคยใช้ django non-rel deploy ใน Google App Engine ตอนนั้นยังไม่รองรับ relational database มีแต่ datastore เป็น nosql 

ปัจจุบัน appengine SDK มี builtins: - django_wsgi มาให้พร้อมใช้งาน เลยอยากลองทำ prove concept  Google Cloud SQL ดูซะหน่อย

การติดตั้ง Google App Engine SDK + Django ใน Development Server



Prerequisition : สมัครใช้งาน Google Cloud SQL - Sign up for Limited Preview (estimate time: 7-10 days)

Download / Runtime version ที่ GAE รองรับ (updated 04/2012)

9 กุมภาพันธ์ 2555

ปัญหาขนาด database log file .ldf โตไม่หยุด MSSQL server

file .ldf  เป็นไฟล์ที่แยกเก็บบันทึก transaction  ของไฟล์ฐานข้อมูล (.mdf file) และจำเป็นต้องใช้งานคู่กัน  ถ้าหาก transaction log ไม่สมบูรณ์หรือเราจัดการผิดวิธี sql server จะเรียกใช้งาน database ก้อนนั้นไม่ได้เลย

สาเหตุที่ไฟล์ .LDF ขนาดใหญ่ขึ้นไม่หยุด

ปกติค่า default ของฐานข้อมูลใน SQL server จะอยู่ใน full recovery mode แปลว่าถ้าเราไม่ทำอะไรซักอย่างกับ database ก้อนนี้ ขนาดของไฟล์ .ldf ก็จะโตขึ้นเรื่อยๆ ทุกวันจนฮาร์ดดิสเต็มในที่สุด

ในการติดตั้ง SQL Server ไม่ได้มี options นี้ให้เราเลือก admin หลายคนกด next >> ไปไม่โดยไม่สนใจบางทีก็คิดไปเองว่า การติดตั้ง database ลงในเซอฟเวอร์เป็นหน้าที่ของผู้พ้ฒนาโปรแกรมน่าจะรู้เรื่องมั้ง (พลาดแล้ว..)

วิธีแก้ปัญหาขนาดไฟล์ .LDF


1. ตั้งค่า recovery model ของ database เป็น simple เพื่อให้ database หยุดขยายขนาด log แบบไม่มี limit
  • ใช้ SQL Server Management Studio  login เข้าไปจัดการเซอฟเวอร์ฐานข้อมูล
  • ใน Object Explorer browse ไปที่ไฟล์ database ที่ต้องการ Right click > Properties > Options เลือก Rocovery model : Simple 


17 มกราคม 2555

JQuery + JSON data เรียงลำดับไม่เหมือนกันใน Browser Firefox, IE, Chrome

วันนี้เจอปัญหาใช้ jquery ดึงข้อมูลจาก php array เป็น json แล้วใน IE / firefox / chrome เรียงไม่เหมือนกัน

$.ajax({
                    type: 'POST',
                    url: $('#ProductForm').attr('action'),
                    data: $('#ProductForm').serialize(),
                    dataType: 'json',
                    beforSend: function(){
                        $('#status').addClass('loading');
                    },
                    success: function(data){
                        console.log("%s", "success callback generate HTML option..");
                        
                        if(data != null){
                            //$('#ProductId').html('').show();
                            $.each(data.products, function(key, value){
                                optionTag = '';
                                console.log("appending : %s", optionTag);
                                //$('#ProductId').append(optionTag)
                            });
                        }
                        $('#status').removeClass('loading');
                    },
                    error: function(xhr, status){
                        //$('#ProductId').hide();
                        $('#status').removeClass('loading');
                        console.log("Ajax error status code: %d text: %s", xhr.status, xhr.statusText)
                    }
                });

ในฝั่งเซอฟเวอร์ SQL query ข้อมูลโดยกำหนด ORDER BY `reference` ASC ให้เรียงตามรหัสสินค้า
แล้วใช้ PHP json_encode() response กลับมาเป็น json key/value หน้าตาแบบนี้

ตัวอย่าง response ข้อมูลเรียงลำดับตามชื่อ
{"products":{"1040":"16PHOTO","1037":"16RELAY","1035":"8RELAY","1034":"8SS R\/L out","1024":"AD\/DA","1005":"USB Starter"}}