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.

// 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');

});

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:

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.

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

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.

Thank you Paul,

You gave me the logic:) and idea.

OOP is nice:)