3

I'm developing an application for mobile devices using the DHTMLX-touch framework, which is basically javascript.

I have a class A where this function is implemented:

A.prototype.initEventHandler = function(controller) {
    $$('btn_submit').attachEvent('onItemClick', controller.switchView($$('ui_ewmon_main')));
}

First implementation

the object controller is an instance of the class B, and the class B is the following:

function B() {     
    //back button management
    this.last_view=new Array();
    this.current_view=this.ewmon_view.getRoot();

    this.switchView = function(next) {
        this.last_view[this.last_view.length]=this.current_view;
        this.current_view=next;
        next.show();
    }
}

with firebug i get this error:

A.js:32Uncaught TypeError: Object # has no method 'switchView'

Second implementation

if I try to define switchView function as a prototype function:

function B() {     
    //back button management
    this.last_view=new Array();
    this.current_view=this.ewmon_view.getRoot();
}
B.prototype.switchView = function(next) {
    this.last_view[this.last_view.length]=this.current_view;
    this.current_view=next;
    next.show();
}

in this case I get the following error:

B.js:13Uncaught TypeError: Cannot read property 'length' of undefined

Can anyone help me?

Thanks in advance Danilo

2
  • 1
    Are you using new to create the instance of B? Commented Oct 20, 2011 at 13:58
  • im creating the Bobject in the constructor of A object as: this.b = new B(); anyway the Prusse's solution worked for me. Thanks for your help Commented Oct 21, 2011 at 14:02

1 Answer 1

3

You are calling attachEvent with the return of controller.switchView($$('ui_ewmon_main')), to get the result you seen to want do:

A.prototype.initEventHandler = function(controller) {
    $$('btn_submit').attachEvent('onItemClick', function(){ controller.switchView($$('ui_ewmon_main')); });
}

Edit:

Looking at the code you posted I will expect this error to show up. From the line this.current_view=this.ewmon_view.getRoot(); one will guess this.current_view is always an object like the one returned by EWmonView.prototype.getRoot(rigth?), but this object don't have a method show.

But you call controller.switchView($$('ui_ewmon_main')); causing this.current_view to be whatever is $$('ui_ewmon_main'), keeping different object types in the array this.last_view and this.current_view is the source of the mistake.

A dirty fix will be:

if (this.current_view.show) {
    this.current_view.show();
} else{
    $$('ui_ewmon_main').show(); //or $$('ui_ewmon_login').show() can tell what you want =)
}

In the place of the occurrences of this.current_view.show(); in EWmonController. But rework the code and get this.current_view to have always the same kind of objects looks like better to me.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Prusse,the solution worked well. Now I added the back function in the initEventHandler method: $$('btn_back').attachEvent('onItemClick', function(){ controller.lastView(); }); and in the B object I added: B.prototype.lastView = function() { this.current_view=this.last_view[this.last_view.length-1]; delete this.last_view[this.last_view.length-1]; this.current_view.show(); } when I click on btn_submit I can switch the view (calling the method show()), but after, when I click on btn_back I get B.js:23Uncaught TypeError: Object #<Object> has no method 'show' Help please.
Hi Prusse, you can find the 2 classes at this address the A class and here the B class. Tjanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.