javascript - Prevent method from returning 'undefined' -
i'm new callbacks , trying working. don't want getcustomeriddescription returning popover until post has returned data, error on 'callback(supplierid)' line @ bottom says 'callback not function' how write callback nothing gets returned getcustomeriddescription until have post data?
here code
scope.showcustomeridlist = function(value) { $('#{0}'.format(value)).popover({ html: true, container: 'body', content: function() { return scope.getcustomeriddescription(value); }, title: function() { return 'customer id - description'; } }).popover('show'); }; scope.getcustomeriddescription = function(supplierid, callback) { var model = {}; model.clientid = scope.contextclientid; model.supplierid = supplierid; $.post(scope.enumcontrollers.getcustomeridsforsupplier, model, function(response) { if (response.error == false) { var ids = json.parse(response.ids); var list = ''; _.each(ids, function(result) { list += '<li>' + result.customerid + ' - ' + result.customerdescription + '</li>'; }); return '<ul>' + list + '</ul>'; } else { return "cound't fetch customer ids"; } }).fail(function() { return "cound't fetch customer ids"; }); callback(supplierid); };
you call:
scope.getcustomeriddescription(value);
but define:
scope.getcustomeriddescription = function(supplierid, callback) {
since don't pass value, callback
undefined
.
then you:
callback(supplierid);
… without testing see if callback
function or not.
either:
- remove line or
- wrap in test makes sure function passed or
- always pass function second argument when call
getcustomeriddescription
incidentally, there isn't point in passing callback function there if call ajax request sent. makes more sense put inside callback functions set on request.
Comments
Post a Comment