views:

386

answers:

2

Hi,

I want to change the method DomElement.appendChild() to work differently when applied on an Object that I have created:

// using MooTools;
var domElement = new Class({...});

var MyObject = new domElement(...);

$(document).appendChild(MyObject); // Error

// I want to add to (extend) appendChild, to do something specific, when:
alert(MyObject instanceof domElement); // true

I could do this by modifying the mootools.js, but I want to avoid this at all costs, because I am certain one day, some other developer will overwrite the js file with an updated version, and I will be imprisoned for murder.

Please, keep me out of jail!

A: 

I dont know about MooTools, but there is a simple way in native JS to do this..

    var orgAppendChild = document.appendChild;  
    document.appendChild = localAppendHandler;


    var domElement = new Class({...});
    var MyObject = new domElement(...);
    // document.appendChild(MyObject);

     var ss = document.createElement('tr'); 
document.appendChild (ss);


    function localAppendHandler(MyObject)
    {
     if (MyObject instanceof domElement)
  UrCustomRoutine(MyObject )
        else
         orgAppendChild(MyObject);


    }

    function UrCustomRoutine(MyObject ){
 //your custom routine
    }

Hope this helps

Update: From your further explanation (handling appendChild of any DOM element), i understand that there is no generic way of defining local hanlders to trap the event.

But there is a workaround (this is very ugly i know). Means you have to define a LocalHandler for the DOM element before you are goin to use the appendChild .

Before doing this document.getElementByID('divTarget').appendChild()

you have to reassign the localhandler to the element you need to access

    orgAppendChild = document.getElementById('divTarget').appendChild;  
    document.getElementById('divTarget').appendChild = localAppendHandler;
    var sss = document.createElement('table'); 
    document.getElementById('divTarget').appendChild(sss);

Another Alternative: If you wanted to keep it simple, i suggest this alternate way. Create AppendChild local method which accepts object and the node name as param.

    function AppendChild(MyObject ,Node)
    {
           if (MyObject instanceof domElement)
               //your custom routine
           else if (Node=="" && (MyObject instanceof domElement==false))
               document.appendChild(MyObject);
           else if (Node!="" && (MyObject instanceof domElement==false))
               eval( "document.getElementById('" + Node + "').appendChild(MyObject);");


    }

And

  1. If you wanted to append DOM element to document

     AppendChild(DOMelement,"")
    
  2. If you wanted to append DOM element inside other container

     AppendChild(DOMelement,"divTarget")
    
  3. If you wanted to process your custom object

     AppendChild(customObject,"")
    

this is the best way to do this.

Cheers

Ramesh Vel

Ramesh Vel
Ramesh, it's so simple that it's brilliant. I will check if there is any problem with mootools and this solution.
Theofanis Pantelides
Glad it helped :-)
Ramesh Vel
Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: "http://10.0.0.11/lab/theo.aspx Line: 490"]
Theofanis Pantelides
it completely overlooks it.
Theofanis Pantelides
i dont know what it means.. i have tested here.. it works fine... i have updated script,, pls check out
Ramesh Vel
OK i found the problem, maybe you can help:with your implementation if I do:document.appendChild() it worksif I do: document.getElementByID('divTarget').appendChild() doesn't work
Theofanis Pantelides
I want to override *.appendChild()
Theofanis Pantelides
check out my uodate...
Ramesh Vel
What do you think if I make a list of overrides:HTMLDivElement, etc...Would this work?
Theofanis Pantelides
Guys pls leave a feedback why you downvote.... the above method works as OP expected (from his initial question)... Run the code yuorself before you downvote it....
Ramesh Vel
@Theofanis, i have added another approach.. check out this
Ramesh Vel
Hi Ramesh, Thank you for this.I know how to create my own function for this, I just want to override the native way.
Theofanis Pantelides
I don't mean to be rude.. but it seems that both you and Theofanis are trying to bend over backwards to cobble a solution using `appendChild`, when the simplest answer is to use a _separate_ function, as in your `AppendChild`
K Prime
The point of this question is to extend appendChild; Had that not been the point, this question would not have been posted here today.
Theofanis Pantelides
this is not very mootools - what's the point in using the framework when you go around the methods that are already provided...
Dimitar Christoff
Although this isn't the answer I was looking for, it'll do.
Theofanis Pantelides
A: 

you ought to look at using object.toElement() in your class

http://blog.kassens.net/toelement-method

then just use document.body.inject(instanceObject);

Dimitar Christoff
why is this being downvoted - it is the correct and proper way to do it through mootools as provided by the framework. here's another example of class to element: http://bit.ly/5BOQ8s
Dimitar Christoff