SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Declare seperate callback functions to Objects

    Hello,

    I am new to Object oriented. But i am triying to improve myself.

    My problem;

    I have many forms on a page. When a user click on a new or upate link, releated forms shows up in a dialog box and i validate it .

    What my problem is i after i validate the forms i want them to go to it's own callback. Every callback functions is different according to forms.

    How can i set different callbacks to each process.

    Code below isn't work exactly. It always alert "test1";

    Thank you.

    Code JavaScript:
    // JavaScript Document
     
    var process = function() {
    	this.url ="";
    	this.panel="";
    	this.panel_title='';
    	this.panel_button='';
    	this.callBack();
    	this.init();
    }
     
    process.prototype = {
    	init : function() {		
    		//some stuff
    	},
    	PanelRender : function()
    	{
    		var that = this;
     
    		$(this.panel).dialog({
    			autoOpen: false,
    			modal: true,
    			close: function(event,ui)
    			{
    				$(this.panel).remove();
    			}
    		});		
    	},
    	PanelOpener : function(html_id)
    	{
    		this.url = $(html_id).attr('href');
     
    		var that = this;
     
    		$(html_id).click(function(e)
    		{
    			e.preventDefault();
     
    			$(that.panel).dialog("option", "title", that.panel_title);
    			$(that.panel).dialog("option", "buttons", 
    			[
    				    {text: that.panel_button, click:function(){ $(that.panel + " form").submit() }},
    					{text:'Cancel', click:function(e){ $(that.panel).dialog('close');  } }
    			]);
     
    			that.ValidateForm();	
     
    			$(that.panel).dialog('open');
    		});
    	},
    	ValidateForm : function()
    	{
    		var that = this;
    		var form = $(this.panel + ' form');
     
    		$(form).validate({		
    				submitHandler: function(){
    					that.callBack();
    				}
    			});
    	},
    	callBack : function()
    	{
    		//callback function works here
    	}
     
    }
     
     
     
     
    $(document).ready(function() {
     
     
    NewProccess = new process();
    NewProccess.callBack = function(){ alert("test1") };
    NewProccess.panel_button = 'New';
    NewProccess.panel = '#new_panel';
    NewProccess.panel_title = 'Add form';
    NewProccess.PanelRender();
    NewProccess.PanelOpener('.new_feature');
     
    EditProccess = new process();
    EditProccess.callBack = function(){alert("test2")};
    EditProccess.panel_button = 'Update';
    EditProccess.panel = '#update_panel';
    EditProccess.panel_title = 'Update form';
    EditProccess.PanelRender();
    EditProccess.PanelOpener('.edit_feature');
     
    });

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    The problem is that you're destroying the existing prototype for the object. Completely replacing the prototype is a bad idea because it also contains information specifying things like the constructor.


    Instead, you have two different ways that are available to you.

    1. You can define the process methods from inside of the constructor itself:

    Code javascript:
    function Process() {
        ...
        this.init = function () {
            ...
        };
        this.panelRender = function () {
            ...
        };
    }

    If you do it this way, you can then call functions like this.init() from the end of the constructor itself.

    2. The other way is by specifying the prototype methods individually after the constructor.

    Code javascript:
    function Process() {
        ...
    }
    Process.prototype.init = function () {
        ...
    };
    Process.prototype.panelRender = function () {
        ...
    };

    Notice that the constructor has no access to these functions, so you cannot call this.init() from within the constructor.

    Much cleaner ways of creating objects do exist though, such as Object.create, but it requires JavaScript 1.85 which is not yet supported on many web browsers you may care about.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Paul,

    You gave me the logic and idea.

    OOP is nice


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
  •