Monday, June 18, 2012

Replace all the matching String in Javascript

Problem: How to replace all the matched String with your String in Javascript?

Solution:
Use replace function of Javascript to replace all Strings in below format.

String.prototype.replaceAllString = function(stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

Use it as below:
var newString = oldString.replaceAll("StringtoBeReplaced", "StringToBeReplacedWith");

Thursday, June 7, 2012

How to create a OK CANCEL alert prompt in CQ

Problem: The problem was to change the simple alert into OK CANCEL Message Box in CQ ExtJS and to have different functionality on the basis of clicked button?

Solution:
Simple alert functionality is written as:
CQ.Ext.MessageBox.alert(
                                 CQ.I18n.getMessage("Title"),
                                 CQ.I18n.getMessage("Message"),
                                 function(){
                                          //User defined functionlity
                                 }
);

In order to turn it into OK CANCEL prompt use the below code
CQ.Ext.Msg.show( {
                          title : CQ.I18n.getMessage("Title"),
                          msg : CQ.I18n.getMessage("Message"),
                          buttons : CQ.Ext.Msg.OKCANCEL,
                          fn : function(buttons) {
                              if(buttons == "ok")
                                  //User defined functionlity
                              },
                            icon : CQ.Ext.MessageBox.QUESTION
                      });


For further reference you can check the below reference from Adobe Day CQ5 link:
http://dev.day.com/docs/en/cq/current/widgets-api/index.html
and Search using the key as  "MessageBox"