Pages

Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Friday, September 30, 2016

Angular Js- Using minified version of Controller,filter js files

Using minifed version of controller JS Files

Here we will use a controller as an example same can be used for Filters

One important thing to note when we isolate controller from our HTML file is during its minification.
The minification also minifies variable in controller and $scope,$http,$filter may become variables a,b,c which are not recognized by angular see the minified version of above controller.js is as follows

controller.js

 angular.module('myApp',[]).controller('myCtrl',function($scope){  
 $scope.names=[  
 {name:'Gaurav',country:'India'},  
 {name:'Mike',country:'USA'},  
 {name:'Steve',country:'Denmark'}  
 ];  
 });  

controller.min.js

 angular.module("myApp",[]).controller("myCtrl",function(n){n.names=[{name:"Gaurav",country:"India"},{name:"Mike",country:"USA"},{name:"Steve",country:"Denmark"}]});  

Here $scope becomes n and this will not be recognized by angular js.To over come this problem use code controller code in controller.js as

controller.js

 angular.module('myApp',[]).controller('myCtrl',["$scope",myCtrl]);  
 function myCtrl($scope){  
 $scope.names=[  
 {name:'Gaurav',country:'India'},  
 {name:'Mike',country:'USA'},  
 {name:'Steve',country:'Denmark'}  
 ];  
 };  

In this we isolate the Controller function but we also tell the module that the first parameter of this function will be "$scope"

The Minified version of this file is

controller.min.js

 function myCtrl(n){n.names=[{name:"Gaurav",country:"India"},{name:"Mike",country:"USA"},{name:"Steve",country:"Denmark"}]}angular.module("myApp",[]).controller("myCtrl",["$scope",myCtrl]);  

Above file also changes the parameter $scope of the function to "n" but when final call is made to function the first parameter is sent as $scope

Saturday, September 24, 2016

Angular Js- Use a filter in expression with javascript addition operator

Recently in one of my Projects I was stuck up in an issue regarding filters

Here is My Custom Filter

       app.filter('alterCap',function()   
       {  
         return function(x)   
         {  
           var i, c, txt = "";  
           for (i = 0; i < x.length; i++) {  
             c = x[i];  
             if (i % 2 == 0) {  
               c = c.toUpperCase();  
             }  
             txt += c;  
           }  
           return txt;  
         };  
       });  

I am trying to use this filter in my HTML as

 {{ x.country|alterCap + ":" + x.capital|alterCap }}  

But this didn't worked out so I had to use filter in my controller.Here is how I did the same

 app.controller("myCtrl",function($scope,$filter){  
         $scope.capitals = [  
         {country:'India',capital:'Delhi'},  
         {country:'Sri Lanka',capital:'Colombo'},  
         {country:'Afganistan',capital:'Kabul'},  
         {country:'Bhutan',capital:'Norway'},  
         {country:'Nepal',capital:'Thimphu'},  
         {country:'Japan',capital:'Tokyo'},  
         {country:'China',capital:'Beijing'},  
         {country:'Russia',capital:'Moscow'},  
         {country:'USA',capital:'Washington, D.C.'}  
         ];  
         $scope.applyAlterCap = function(movie){  
           return $filter('alterCap')(movie);  
         };  
       });  

And then I used this in my Html as follows

 <p>Using Custom Filter with addition operator inside expression</p>  
     <ul>  
       <li ng-repeat="x in capitals | orderBy:'capital'">  
         {{ applyAlterCap(x.country) + ":" + x.capital }}  
       </li>  
     </ul>  

Sunday, August 24, 2014

ZendX-Use events and properties of jquery-ui widgets in Zend forms

Recently I added a ZendX Date Picker to one of my Zend Applications now I was able to add properties but I was unable to add events so I had to use another Zend magic function
Zend_Json_Expr

As shown in code below

    /*************Order Date From****************************************/     
         $order_date_from = new ZendX_JQuery_Form_Element_DatePicker('order_date_from',  
               array('jQueryParams' => array('dateFormat' => 'yy-mm-dd',  
                 'onClose'=>new Zend_Json_Expr('function( selectedDate ) {$( "#order_date_to" ).datepicker( "option", "minDate", selectedDate );}')  
                 ))  
           );  
         $order_date_from->setAttribs(array('class'=>"",  
             //'id'=>'datepickerfrom'  
         )  
           );  
         $order_date_from->removeDecorator('DtDdWrapper')  
           ->removeDecorator('HtmlTag')  
           ->removeDecorator('Errors')  
           ->removeDecorator('Label');  
         $this->addElement($order_date_from);  
         /*************Order Date From****************************************/  
         /*************Order Date To****************************************/  
         $order_date_to = new ZendX_JQuery_Form_Element_DatePicker('order_date_to',  
               array('jQueryParams' => array(  
                 'onClose'=>new Zend_Json_Expr('function( selectedDate) {$( "#order_date_from" ).datepicker( "option", "maxDate", selectedDate );}'),  
                 'dateFormat' => 'yy-mm-dd'              
                 ))  
           );  
         $order_date_to->setAttribs(array('class'=>"",  
             //'id'=>'datepickerto'  
         )  
           );  
         $order_date_to->removeDecorator('DtDdWrapper')  
           ->removeDecorator('HtmlTag')  
           ->removeDecorator('Errors')  
           ->removeDecorator('Label');  
         $this->addElement($order_date_to);  
         /*************Order Date To****************************************/  

Friday, November 29, 2013

Javascript-Fire event on resolution change

In the era of responsive websites sometimes it is needed to fire an event on change of resolution.

My designer was stuck in one such scenario and we sorted the same out as follows


 <script>  
 $(document).ready(function () {  
 var width = screen.width,  
 height = screen.height;  
 setInterval(function () {  
 if (screen.width !== width || screen.height !== height) {  
 width = screen.width;  
 height = screen.height;  
 $(window).trigger('resolutionchange');  
 }  
 }, 50);  
 }());  
 $(window).bind('resolutionchange', heightchanges)  
 function heightchanges(){  
 /*Your code on resolution change event here*/
 }  
 });  
 </script>  

Sunday, April 21, 2013

Stop a link from reloading page

Place Javascript:void(0) for the href property.

or Javascript:return 0 and your page will not reload it self on click of the link.

Sunday, October 21, 2012

Use the javascript element for jquery functions

I was working on one of the functions and had pass an js element to it and wanted to perform jquery functions which require an id in format $("#id")

Here is how to use jquery operations on an element received in js format


//ele is an checkbox element
function enabletext(ele)
{
    if(ele.checked==true)
    {
        var myid=ele.id;
        jQuery(eval("'#" + myid + "'")).parent().siblings("#qtyid").show();
    }
}

Thursday, August 23, 2012

Synchronous Javascript using event dispatchers

 if(!(resp['Indiform2']===undefined))
                        $(document).bind('pdf2',function(){
                         generatepdf('individualaccountpdf','pdf2',resp);
                        });

Bind The event as per requirements and need

And then execute it when every thing is done with

 if(filename=='InvestmentAcoountAgreement')
 $(document).trigger('pdf2');

Saturday, August 4, 2012

Simple Javascript Accordion

http://www.switchonthecode.com/tutorials/javascript-and-css-tutorial-accordion-menus

Sunday, July 8, 2012

Javascript code to get a string sliced till last occurance of a charcter

Javascript code to get a string sliced till last occurrence of a character function
 //javascript
getStringTillLastCharcter(str,chr)
{
    var flag=0;
    var lastpos;
    //alert(str.length);
    for(i=0;i < str.length; i++ )  {
        if(str.charAt(i)==chr)
            {
                lastpos=i+1;
            }
    }
    //alert(lastpos);
    return str.slice(0,lastpos);
}


Here is a code to get the string till the last occurance of a charcter.....

Javascript to automatically add wpmode behind your video url

//javascript
$(document).ready(function()
{
    $('#id_of_iframe').each(function()
    {
        var url = $(this).attr("src"winking smiley
        $(this).attr("src",url+"?wmode=transparent"winking smiley
    }
    );
}
;

Tuesday, July 3, 2012

Check if java script is disabled

use
"noscript" html tag

check here

Thursday, August 11, 2011

Dynmically creating a table using Javascript and adding rows to it

For this you need to first of all define an empty html table element with an id property at least to refer to it my dummy was.
table
tr
th /th
th /th
/tr
tr
td /td
td /td
/tr
/table

Written without tags so that it is not interpreted by blog as html.

My table has a header row and a normal row

The table rows starts from index 0.You can use following code from a js file or directly on an html file.
(I recommend js file helps in re usability of code)

var tbl=document.getElementByid('tbl');
//Get the last row

var lastrow=tbl.rows.length

//since I already have a header row in my table and a row manually added so in my case //new row will be equal to last row (table length does not includes the last row)

var iteration=lastrow;
//create a new row
row=tbl.insertRow(lastrow);

//leftcell
//general text node to show the Sno's
var cellleft=row.insertcell(0);
var textnode=document.CreatetextNode(value of the node);
cellleft.appendChild(textnode);

//rightcell
//creation of a textbox node
var cellright=row.insertCell(1);
var el=document.CreateElement('input');
el.type='text';
el.name='name';
el.id='id';
cellright.appendChild(el);

you can add more columns by incrementing index in var cellright=row.insertCell(1);

One of my columns required to create a list dynamically..since I need the whole list and so I cloned the same from my first row(it was the only need for which I had to create a row manually)

To clone an element use the following
elsource=document.getElementById('list element');
eldestination=elsource.clone();
eldestination.name='new list name'
eldestination.id='new id'

So that's how we can dynamically add rows to a table....