Friday, 10 October 2014

Simple Coding Rules

1. Code should be proper indented.
2. Meaning full name of variables and constants.
3. Meaning full name of methods and classes.
4. No duplicate code.
5. No inline css.
6. No inline java script.
7. Html, css and java script code also be indented.
8. No html code should come with java services.
9. If you find a code two times in a class than make a separate method using that code.
10. If you find same code in two or more classes than make a separate class using that code and use       that class.

Friday, 1 August 2014

AngularJs: order by filter

Use order by like this:

<tr ng-repeat="emp in empList | orderBy:joiningDate">
</tr>

Above code will show you emp list in ascending order of joining date.

<tr ng-repeat="emp in empList | orderBy:-joiningDate">
</tr>

You can see I used - (hyphen) in above code. Due to this hyphen employee list will show in descending order of joining date.

Thursday, 31 July 2014

How to show alternate image if source image is not found?

A example is here

Phonegap/Cordova: Download file

To download file in cordova you have to add following plugins:

1. File
2. File System
3. File Transfer

And example is here:
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, app.fileSystemSuccess, app.fileSystemFail);
        console.log('Received Event: ' + id);
    },

fileSystemSuccess: function(fileSystem) {
var directoryEntry = fileSystem.root;
directoryEntry.getDirectory("folder_rahul", {create: true, exclusive: false}, app.onDirectorySuccess, app.onDirectoryFail);
var rootdir = fileSystem.root;
var fp = rootdir.toURL();
alert(fp);
fp = fp + "folder_rahul/120px-State-lib-sum.png";
var fileTransfer = new FileTransfer();
   fileTransfer.download("http://data-gov.tw.rpi.edu/w/images/thumb/b/b1/State-lib-sum.png/120px-State-lib-sum.png",fp,
   function(entry) {
       alert("download complete: " + entry.fullPath);
   },
   function(error) {
cosnole.log(error);
       alert("download error source " + error.source);
       alert("download error target " + error.target);
       alert("upload error code" + error.code);
   }
);
},

fileSystemFail: function(evt) {
    alert("Failed---"+evt.target.error.code);
},

onDirectorySuccess: function(parent) {
console.log(parent);
},

onDirectoryFail: function(error) {
alert("Unable to create new directory: " + error.code);
}
};

More details are here and here

Monday, 28 July 2014

Angularjs filtered array length

In this example, I am applying a filter on list and after that I am getting the length.

<tr ng-show="showdate && (yourlist | filter:searchEmp).length > 0"><td class='appointment_header' style="font-weight:bold;" colspan="3"><span> {{----}}</span></td></tr>