Thursday, June 20, 2013

Detecting When DOM Elements Have Been Removed With jQuery

If you look at the W3C, there is actually an event that gets triggered when a DOM element is removed from a document sub-tree:
DOMNodeRemoved: Fires when a node has been removed from a DOM-tree.
This DOM event will bubble up the document tree with the removed node as its target. But of course, even though this is a standard in the W3C, it's not fully supported in the various browsers. And, somewhat to be expected, from my brief testing, the one browser that I have that doesn't support this event type is Internet Explorer. However, if we are going to be using jQuery to perform our DOM mutations, we can actually simulate this event if the current browser is IE.


function common__NodeRemovedEventTrigger() {
    jQuery( "body" ).bind(
        "DOMNodeRemoved",
        function( objEvent ){
            // Append event to log display.
            var elm = jQuery(objEvent.target);
            if(elm.attr("class") !== undefined) {
                console.log("Removed element class name: "+elm.attr("class"));
            }
        }
    );
}

http://www.bennadel.com/blog/1623-Ask-Ben-Detecting-When-DOM-Elements-Have-Been-Removed-With-jQuery.htm 

Wednesday, June 19, 2013

bash split string into array

IFS=', ' read -a array <<< "$string" /* array is the variable name */
To access an individual element:
echo "${array[0]}"
To iterate over the elements:
for element in "${array[@]}"
do
    echo "$element"
done
To get both the index and the value:
for index in "${!array[@]}"
do
    echo "$index ${array[index]}"
done
The last example is useful because Bash arrays are sparse. In other words, you can delete an element or add an element and then the indices are not contiguous.
unset "array[1]"
array[42]=Earth

How to set a BASH variable equal to the output from a command?

In addition to the backticks, you can use $(), which I find easier to read, and allows for nesting.
OUTPUT=$(ls -1)
echo $OUTPUT
 
 
You can use eval to execute a string:
eval $illcommando
 

Monday, June 17, 2013

jQuery render iframe without src and direct html

<iframe id='iframe' frameborder='0' height='200' width='300'></ifraem>

var html = '<div>any html here</div>';

var frame = document.getElementById("iframe').contentWindow.document;
frame.open();
frame.write(html);
frame.close();

Write the following code to get the iframe body using jQuery
var iFrame = jQuery("#iframe").contents().find("body");

To get the window object for a frame you can use the window.frames array:
var iframewindow= frames['iframe_name'];
 
This requires that you give the <iframe> an old-school name attribute instead-of-or-as-well-as the id. Alternatively if you know the order of iframes on the page you can index them numerically:
var iframewindow= frames[0];
 
It's generally more flexible to get the iframe window from the iframe element in the DOM, but this requires some compatibility code to cope with IE:
var iframe= document.getElementById('iframe_id');
var iframewindow= iframe.contentWindow? iframe.contentWindow : iframe.contentDocument.defaultView;

jQuery defines the contents() method to grab the document node, but it doesn't give you a cross-browser way to go from the document to the window, so you're still stuck with:
var iframe= $('#iframe_id')[0];
var iframewindow= iframe.contentWindow? iframe.contentWindow : iframe.contentDocument.defaultView;

Saturday, June 15, 2013

YII use beforeSave and beforeValidate function as common for all Model

Create a file suppose named 'MyActiveRecord.php' suppose in 'components' folder.


<?php
class MyActiveRecord extends CActiveRecord {
    public function beforeValidate()
    {
        if(parent::beforeValidate())
        {
            /* Do some common work */
            return true;
        }
    }
    function beforeSave() {
        if (parent::beforeSave()) {
            if($this->isNewRecord) {
                $maxOrderNumber = Yii::app()->db->createCommand()
                ->select('max(id) as max')
                ->from($this->tableName())
                ->queryScalar();
              $this->id = intval($maxOrderNumber) + 1;
            }
        }
        return true;
    }
}
?>

Now create a Model named 'Email' in 'models' folder.
See the highlighted extends object that is the previous one.
The MyActiveRecord does when a new Email inserted to database, set its id as
current id+1 as new id.
<?php
class Email extends MyActiveRecord {
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function tableName()
    {
        return 'email';
    }
    public function rules()
    {
        return array(
            array('address, contact_id, contact_user_id', 'required'),
            array('contact_id, contact_user_id', 'numerical'),
            array('address, email_label', 'length', 'max'=>250),
            array('email_label, address', 'safe', 'on'=>'search')
        );
    }
    public function attributeLabels()
    {
        return array(
            "address" => "Address",
            "email_label" => "Category"
        );
    }
    
    public function findByParameters($parameters)
    {
        $criteria=new CDbCriteria;
        foreach ($parameters as $key => $value) {
            $criteria->compare($key, $value,true);
        }                
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria
        ));
    }
}
?>

Thursday, June 13, 2013

jQuery clear a file input field

<input type="file" id="control"/>
<button id="clear">Clear</button>

var control = $("#control");

$("#clear").on("click", function () {
    control.replaceWith( control = control.clone( true ) );
});

Wednesday, June 12, 2013

Styling CMenu and CMenu items and links with CSS class in Yii

Many times we would want to style our CMenu by adding id or css classes to it. We can easily do this through htmlOptions, itemOptions, and linkOptions.

Adding id and class names to CMenu

We use the id and htmlOptions to accomplish this. Watch.

//in your view
$this->widget('zii.widgets.CMenu', array(
 'id'=>'myMenu',
 'items'=>$this->myMenu,
 'htmlOptions'=>array('class'=>'span-24 last'),
));
this will make Yii render
<ul class="span-24 last" id="myMenu">
...
</ul>

Adding class names to CMenu items and CMenu item links

We use itemOptions and linkOptions for this. Example.
//in your controller
$this->myMenu = array(
'id'=>'myMenu',
'items'=>array(
 array(
  'label'=>'Home',
  'url'=>array('site/index'),
  'itemOptions'=>array('class'=>'visited'),
  'linkOptions'=>array('class'=>'bar'),
 ),
 array('label'=>'Sign Out', 'url'=>array('site/signout')),
),
);
[ad]
This will let Yii render
...
<ul id="myMenu">
 <li class="visited">
  <a class="bar" href="/site/index">Home</a>
 </li>
...
http://code.dimilow.com/styling-cmenu-and-cmenu-items-and-links-with-css-class-in-yii/