SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2002
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Binding object method to event

    I'm trying to build a Validation object, where you pass an object id and event name in the constructor. Then, you call an add_rule method to add validation rules. The constructor code looks something like:

    function Validation(id, event, ...)
    {
    this.object = document.all[id];
    this.event = event;
    var obj = this.object;
    obj[event] = this.validate;
    }

    So the actual validation function is dynamically bound to the appropriate event. The problem is that when the validate method is called, "this" no longer refers to the Validation object; it refers to the object to whose event it was bound. So, for instance, in the following code, the validate function's "this" refers to frmSubmit, not objValidate:

    objValidate = new Validation ("frmSubmit", "onsubmit", ...);

    Of course, I need to refer to the actual validation object's properties, but I can't figure out how. Any suggestions?

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off: congratulations - this is what i like to see - a real use of functions and object passing

    Now, some info:

    You could return the object in order to get it it created for sure ( though you needn't really, and it's archaic cobol throw back that i definate frown upon ).

    surely though, you are pretty much there (except for trying to go backwards up a stream)

    Code:
    <input name="cat" id="cat">
    <script language="Javascript">
    
    function Validate(id, event, action){
            this.object = document.all[id];
            this.object[event]=eval(action);
            }
    
    function act(){
            alert('called');
            }
    
    var inp = new Validate('cat','onclick','act');
    
    </script>
    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •