Wednesday 18 November 2015

Changes in Node Js Express 5

Removed methods and properties:

Changed:
Improvements:

Removed methods and properties

If you use any of these methods or properties in your app, it will crash. So, you’ll need to go through and change your app once you update to version 5.

app.del()

Express 5 no longer supports app.del(). Using it will throw an error. For registering HTTP DELETE routes, use app.delete() instead.
Initially del was used instead of delete considering delete is a reserved keyword in JavaScript. However, as of ECMAScript 6, delete and other reserved keywords can legally be used as a property names. You can read the discussion which lead to the deprecation of app.del here.

app.param(fn)

The app.param(fn) signature was used for modifying the behavior ofapp.param(name, fn). It has been deprecated since v4.11.0, and Express 5 no longer supports it at all.

Pluralized method names

The following method names have been pluralized.  In Express 4, using the old methods resulted in a deprecation warning.  Express 5 no longer supports them at all:
  • req.acceptsCharset() is replaced by req.acceptsCharsets().
  • req.acceptsEncoding() is replaced by req.acceptsEncodings().
  • req.acceptsLanguage() is replaced by req.acceptsLanguages().

Leading colon (:) in name for app.param(name, fn)

A leading colon character (:) in name for app.param(name, fn) is remnant of Express 3, and for the sake of backwards compatibility, Express 4 supported it with a deprecation notice. Express 5 will silently ignore it; use the name parameter without prefixing it with a colon.
This should not affect your code, if you have been following the Express 4 documentation of app.param, as it makes no mention of the leading colon.

req.param(name)

This potentially confusing and dangerous method of retrieving form data has been removed. You will now need to specifically look for the submitted parameter name inreq.paramsreq.body, or req.query.

res.json(obj, status)

Express 5 no longer supports the signature res.json(obj, status).  Instead, set the status and then chain it to the res.json() method like this:res.status(status).json(obj).

res.jsonp(obj, status)

Express 5 no longer supports the signature res.jsonp(obj, status).  Instead, set the status and then chain it to the res.jsonp() method like this:res.status(status).jsonp(obj).

res.send(body, status)

Express 5 no longer supports the signature res.send(obj, status).  Instead, set the status and then chain it to the res.send() method like this:res.status(status).send(obj).

res.send(status)

Express 5 no longer supports the signature res.send(status), where status is a number. Instead, use res.sendStatus(status), which sets the HTTP response header code and sends the text version of the code: “Not Found,” “Internal Server Error,” and so on.
If you need to send a number using res.send(), quote the number to convert it to a string, so that Express does not interpret it as an attempt at using the unsupported old signature.

res.sendfile()

res.sendfile() has been replaced by a camel-cased version res.sendFile() in Express 5.

Changed

app.router

The app.router object, which was removed in Express 4, has made a comeback in Express 5. In the new version, it is a just a reference to the base Express router, unlike in Express 3, where an app had to explicitly load it.

req.host

In Express 4, req.host incorrectly stripped off the port number if it was present. In Express 5 the port number is maintained.

req.query

In Express 4.7 and Express 5 onwards, the query parser option can accept false to disable query string parsing, and instead use your own function for query string parsing logic.

Improvements

res.render()

This method now enforces asynchronous behavior for all view engines, avoiding bugs caused by view engines which had a synchronous implementation and violated the recommended interface.

Thursday 1 October 2015

directive link prelink & compile & controller

var app = angular.module('app', []);
app.directive('dad', function () {
    return {
        restrict: 'EA',
        template: '<div class="dad">{{greeting}}{{name}}'+
        '<son></son>'+
        '</div>',
        link: {
            pre: function(scope,elem,attr) {
                scope.name = 'Paul';
                scope.greeting = 'Hey, I am ';
            }
        }
    };
});
app.directive('son', function () {
    return {
        restrict: 'EA',
        template: '<div class="son">{{sonSays}}</div>',
        link: function(scope,elem,attr){
            alert("son");
            scope.sonSays = 'Hey, I am son, and my dad is '+ scope.name;
        }
    };
});

      app.directive('compileExample', compileExample);

    function compileExample() {
      return {
        restrict: 'E',
        scope:{
            type:"="
        },
          bindToController:true,
   controllerAs:'vm',
   controller:function($scope) {
       alert("controller");
   },
        compile: function(tElement, tAttrs) {
            if (tAttrs.type === "new") {
                 angular.element(tElement).append("My new name is {{name}}");
            } else {
                angular.element(tElement).append("My name is {{name}}");
            }
          alert("compile"+tAttrs.type);
          return function postLink(scope, element, attrs) {
              alert("compile postLink");
            scope.name = 'David'
          }
        }
      }
    }

Thursday 3 September 2015

create dynamic dropdown list with time interval

 function populateList(aList, aStartHour, aEndHour) {  
   var itemIndex = 0;  
   // firstly empty the list if it's got anthing in it.  
   while (aList.options.length > 0) {  
    aList.options.remove(0);  
   }  
   // now add the new items;  
   for (var h = aStartHour; (h < aEndHour); ++h) {  
    var hs = ((h < 10) ? "0" : "") + h;  
    // do the fifteen minute thing  
    for (var m = 0; (m < 60); m += 5) {  
      var opt = document.createElement("OPTION");  
      var ms = ((m < 10) ? "0" : "") + m;  
      aList.options.add(opt);  
      opt.value = "time" + itemIndex;  
      opt.innerText = hs + ":" + ms;  
      ++itemIndex;  
    }  
   }  
   return;  
 } 

Example

populateList(document.getElementById("hlist"), 8, 12);

 

Thursday 9 April 2015

how to use q.js promises to work with multiple asynchronous operations

function addAsync(a, b) {
    var deferred = Q.defer();
    // Wait 2 seconds and then add a + b
    setTimeout(function() {
        deferred.resolve(a + b);
    }, 2000);
    return deferred.promise;
}

Q.all([
    addAsync(1, 1),
    addAsync(2, 2),
    addAsync(3, 3)
]).spread(function(result1, result2, result3) {
    console.log(result1, result2, result3);
});
// logs "2 4 6" after approximately 2 seconds

Tuesday 17 February 2015

Storage Events in javascript

Storage Events

The storage objects fire events which your appliation can listen for. A storage event is fired when you insert, update or delete a session or local storage property.
The storage event is only emitted in other browser windows than the one that inserted, updated or deleted the session or local storage objects.
For session storage this means that events are only visible in pop up windows and iframes.
For local storage, events are visible to all other windows open with the same origin, including pop up windows and iframes



           $(window).unbind('storage').bind('storage', function (e) {
                if (e.originalEvent.key === 'usersession') {
                    if (e.originalEvent.newValue !== e.originalEvent.oldValue) {
                        if (!e.originalEvent.newValue) {
                            window.location = '/';
                        }
                        else {
                                //console.log('storage change event : ' + e.originalEvent.newValue);
                        }
                    }
                }
                else if (e.originalEvent.key === 'requests') {
                       console.log(e.originalEvent.newValue);
                }
                else if (e.originalEvent.key === 'activities') {
                    if (e.originalEvent.newValue) {

                    }
                }
            });