Coding in Web Client

2014-02-12  本文已影响103人  liyinkan

Coding with Javascript


be aware of toFixed function

be aware of this

Test

    function test() {
      console.debug(this)
    }
    
    function test_class_A() {
      this.test = test;
    }
    
    function test_class_B() {
      this.test = test;
    }
    
    var cls1 = new test_class_A(),
      cls2 = new test_class_B();
    cls1.test() // output: test_class_A
    cls2.test() // output: test_class_B

prefer doing calculations on Server

extend javascript native functions

authentication and authorization

cache solution

compress and cache statics resources

load by module / lazy loading

define namespace

build common utilities

build page by Components

build common ui components with business first

construct page by Components not by Templates and Logic

how to load User Context

decouple between pages and modules

design navigation first

define global constants

do all AJAX request by common utility

maintain API documents

do unit tests

<script src="./resources/testharness.js"></script> <br />
<script src="./resources/testharnessreport.js"></script>

    test(function() {
      var request = window.indexedDB.open("TestDB")
      var resp = window.indexedDB.deleteDatabase('TestDB')
      assert_not_null(resp)
    }, 'test delete db')
    
    test(function() {
      var req = window.indexedDB.open("TestDB")
      assert_not_null(req)
      req.onerror = function(event) {
        assert_unreached('open error', event.target.errorCode)
      }
      req.onsuccess = function(event) {
        assert_not_null(req.result)
      }
    }, 'open db with callbacks')

  /* set divX background to white when the buttonX is clicked */
  var divX = $('#divX')        // var divX = Ext.getCmp('divX')
  var buttonX = $('#buttonX')  // var buttonX = Ext.getCmp('buttonX')
  
  buttonX.click(function(){    // buttonX.on('click',function(){
    updateBackground();      //   updateBackground();
  })                           // })
  
  function updateBackground() {
    divX.setBackground('#fff')
  }

changed to

    /* set divX background to white when the buttonX is clicked */
    var divX = $('#divX')        // var divX = Ext.getCmp('divX')
    var buttonX = $('#buttonX')  // var buttonX = Ext.getCmp('buttonX')
    
    buttonX.click(function(){     // buttonX.on('click',function(){
      updateBackground(divX);   //  updateBackground(divX);
    })                            // })
    
    function updateBackground(divX) {
      divX.setBackground('#fff')
    }

test could be

    test(function() {
      var div = $('<div></div>')
      updateBackground(div)
      assertEqual(div.getStyle().getBackground(), '#fff')
    }, 'test set background')

more references

上一篇下一篇

猜你喜欢

热点阅读