0

I have a function searchKeyboardCmd which is binded as an event handler to the textbox. Its purpose is to check the data in that textbox is unique. If no it should break execution of this handler and show alert window. If unique it should get data from other texboxes and store it in array ( code fragment from line `var a=new ())

self.searchKeyboardCmd = function (data, event) {
        if (event.keyCode == 13) { //checking if enter was pressed or other key
            foo(function (result) {
                if (result == 'false') {
                    alert("Numer seryjny nie jest unikalny");
                    return true;
                }
            });

            var a = new Eq();
            a.StorageId(self.StorageTemp());
            a.StartDate(self.StartDateTemp());
            a.DeviceSerialNumber(self.Test());
            a.DeviceId(self.DeviceTemp());
            a.Issue(self.Issue())
            a.IssueDesc(self.IssueDesc());
            a.Quantity(self.number());
            a.Project(self.Project());
            a.MeUser(self.MeUser());

            self.data.push(a);
            $('.datepicker').datepicker({ autoclose: true, todayHighlight: true/*, language: "pl"*/, format: 'dd/mm/yyyy' });
            deviceIdField.focus();
            self.Test("");
            return false;
        }
        return true;
    };

My foo function which call back end method. It receives as true from it if unique. False other ways.

function foo(callback) {
    $.ajax({
        url: "/DeviceInstance/IsUnique",
        contentType: "application/json; charset=utf-8",
        type: "POST",
        datatype: "json",
        data: JSON.stringify({ value: viewModel.Test() }),
        error: function (data) {
            alert("Dodanie  nie powiodło się " + data);
        },
        success: function (data) {
            callback(data);
        }
    });

So my problem is in breaking execution of my main event handler method. I tried modifying this lines:

self.searchKeyboardCmd = function (data, event,callback) 

and

foo(function (result) {
                console.log(result);
                callback(result);
            });

The only response I'm getting is : undefined is not a function

szpic
  • 4,346
  • 15
  • 54
  • 85
  • There seems to be no direct syntactical method to achieve what you want. What exactly should happen if `foo` does _not_ return true? – Codor Jun 03 '14 at 07:46
  • if true all code below adding to data array shoudl be execuded if false this should stop – szpic Jun 03 '14 at 07:47
  • Well, you can't do that. You have to restructure your code. `searchKeyboardCmd` has to accept a callback, just like `foo`. – Felix Kling Jun 03 '14 at 07:52
  • Although restructuring my foo function was quite easy I have no idea how to change `searchKeyboardCmd` and still accept event and data as parameters – szpic Jun 03 '14 at 08:06
  • Maybe you don't even have to depending on what the return value actually is used for. But without more context information we can't really give a specific solution. – Felix Kling Jun 03 '14 at 08:31
  • I edited the question with some context and my attempts to fixing my issue – szpic Jun 03 '14 at 08:41

1 Answers1

0

Try this:

var f = foo(function (result) {
    if (result == 'false') {
        alert("Numer seryjny nie jest unikalny");
        return true;
    }
});

if(!f){
    return; // (Or return true or false)
}

If f() returns false, the code below the function call won't be executed, if it returns true, it will be.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • This wont work as result base on output of an ajax call. So if(!f) will be checked before f changes value from undefined – szpic Jun 03 '14 at 07:51
  • @szpic: Why didn't you mention the fact that `foo` makes an AJAX call -.- That _completely_ changes the problem. – Cerbrus Jun 03 '14 at 07:56
  • I want to apologize. I updated question as soon as I realized that I didn't put it in question. – szpic Jun 03 '14 at 08:06