user2454455
user2454455

Reputation:

Prototype function call returns undefined

I am currently studying javascript standard coding patterns. Why does console.dir(dropdownAC.appendImages()) returns undefined, somebody?

In this code I am using dev-bridge autocomplete.js to have an autocomplete widget like homedepot autocomplete where there is a picture box suggestions. If im doing it wrong, feel free to suggest the right modular format that i need to use. For now the images will be on a local storage.

(function ($) {
  "use strict";

  var DropdownAC = function() {
    return this
  }

  DropdownAC.DEFAULTS = {
    products : [
      { value : 'Ash Bucket', data: 'AB', imagePath : 'img/ash_bucket/' },
      { value : 'Drill', data: 'DR', imagePath : 'img/drill/' },
      { value : 'Lights', data: 'LT', imagePath : 'img/light/' },
      { value : 'Shelve', data: 'SV', imagePath : 'img/shelve/' },
      { value : 'Maple Wood', data: 'MW', imagePath : 'img/maple' }
    ]
  }

  DropdownAC.prototype.loadImages = function(key_text){
    return key_text
  }

  DropdownAC.prototype.appendImages = function () {
    var top_result = $('.autocomplete-suggestion:first').text()
    var ac_images = dropdownAC.loadImages(top_result) 
    console.log(ac_images)
  }   

  DropdownAC.prototype.showPictureBox = function(){
    //===============================
    // Create pictureBox
    //===============================

    //--------------HERE------------------//
    console.dir( dropdownAC.appendImages() ) 
  }

  //==================================
  //   VENDOR:autocomplete.js       
  //==================================       
  $('#autocomplete').autocomplete({
    lookup  : DropdownAC.DEFAULTS.products,
    onSelect : function (suggestion){
      alert('What will i do with this?' + suggestion.value + ', ' + suggestion.data);
    }
  })    

  //==================================
  //   Extension-autocomplte tweak    
  //==================================
  $('#autocomplete').keyup(function(){
    var suggestions_visible = $('.autocomplete-suggestions').is(":visible")
    var suggestions_childLength = $('.autocomplete-suggestions').children().length

    if ( suggestions_visible && suggestions_childLength > 0 ){
      dropdownAC.showPictureBox()
    }

    else {
      $('.suggestions_wrapper').remove()
    }
  })

  window.dropdownAC = new DropdownAC();

  $(document)
    .on('load', dropdownAC);

})(window.jQuery);

Upvotes: 0

Views: 323

Answers (2)

Michal Brašna
Michal Brašna

Reputation: 2323

Why does console.dir(dropdownAC.appendImages()) returns undefined, somebody?

Inside functions you connect to prototype use this insteadof dropdownAC and return something like for instance

...
var ac_images = this.loadImages(top_result)
return ac_images;
...

You are using modular pattern, which is fine, just define all dependencies.

(function($, window, document) {
    ...
})(window.jQuery, window, window.document);

It is more clear, what code needs and it helps minification.

Upvotes: 0

Matt Way
Matt Way

Reputation: 33159

Because DropdownAC.prototype.appendImages = function () { does not return anything!

Try changing your appendImages function to this:

DropdownAC.prototype.appendImages = function () {
    return 'hello, i'm here!';
}

Upvotes: 2

Related Questions