locked
Calling a function from an event handler

    Question

  • Hi,

    This is kind of newbie question, but I cannot find how to fix it.

    I have two event handlers for two buttons. Both of them share a piece of code, so I moved this shared code to another method and I called it from both handlers.

    However it failed at runtime because apparently when this handler methods are called, they don't "see" the shared method. I couldn't find my shared method neither in "this" nor in the event properties, although I may haven't looked in the right place.

    Any idea about how to support this basic scenario.

    It is so common than I cannot believe it is easy to achieve.

    Thanks for your help.

    Kind Regards


    Si la respuesta es correcta, por favor márcala como correcta. También puedes votarla como útil si te lo ha sido.

    Wednesday, April 25, 2012 9:25 AM

Answers

All replies

  • Javier, you are correct that it should work.

    Event handlers are just javascript functions and functions can see and call other functions if they are in scope.   Without seeing some code, it is just guesswork.  Can you provide an example?

    Wednesday, April 25, 2012 10:58 AM
  • Hi,

    Sure. Here is the JS code:

    ui.Pages.define("/pages/hub/hubPage.html", {
            removeTask: function (event) {
                var task = retrieveSelectedItem();
                var database = new MetroPomo.Database();
                database.connect()
                    .then(function () { return database.deleteTask(task); })
                    .then(function () { return listView.itemDataSource.remove(currentSelectedItemKey); })
                    .then(function () { Debug.writeln("removed!!"); });
            },
    
            retrieveSelectedItem: function () {
                var listView = document.getElementById("hubTaskListId").winControl;
                var currentSelectedItemKey = listView.currentItem.key;
                var task = TaskData.list.getItemFromKey(currentSelectedItemKey).data;
                return task;
            }
    });

    The event hadler is executed, but when the "retrieveSelectedItem" method is called, I get this error:

    Please, let me know if you need more info.

    Thanks for your help.


    Si la respuesta es correcta, por favor márcala como correcta. También puedes votarla como útil si te lo ha sido.


    Wednesday, April 25, 2012 11:05 AM
  • Ok, it seems that the event handler code is not able to see "retrieveSelectedItem" because all this code is defined inside an anonymous function.

    However, if I move the "retrieveSelectedItem" method outside of the anonymous function, it works correctly.

    On the other side, I don't think that is a good practice. If I were developing this code in a real Object Oriented language, this method would always be private because it doesn't make sense to expose it to the rest of the application code.

    There may be another way to do it without exposing the method publicly.


    Si la respuesta es correcta, por favor márcala como correcta. También puedes votarla como útil si te lo ha sido.

    Wednesday, April 25, 2012 12:18 PM
  • Javier,

    Take a look at the example here.  It shows how the methods and helper functions for a page can be hidden in scope from the rest of the application.

    Wednesday, April 25, 2012 2:44 PM
  • Also see here for function scoping:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh780660.aspx


    Jeff Sanders (MSFT)

    Wednesday, April 25, 2012 4:11 PM
    Moderator