O
O
ODY2015-05-27 12:24:39
JavaScript
ODY, 2015-05-27 12:24:39

Remove error when backbone dropdown is hidden?

you need to hide the dropdown menu on click on document.

events: {
      'click': function() {
        this.countriesDropDownListView.toggleList();
      }
    },


toggleList: function() {
      this.$el.toggle();
      $(document).one('click', function() {this.$el.toggle()});
    }


This whole thing generally works, but throws out this error:
Cannot read property 'toggle' of undefined

Also, I'm not very sure of the correctness of this approach, if there are recommendations, I'll listen.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Melnikov, 2015-05-27
@ODY

toggleList: function() {
  var el = this.$el;
  el.toggle();
  $(document).one('click', function() {
    el.toggle();
  });
}

O
ODY, 2015-05-28
@ODY

Thanks for the answers, indeed both answers are correct, but still I will leave the code where I stopped

toggleList: function(e) {
      e.stopPropagation();
      this.$el.toggle();
      $(document).one('click', function() {this.$el.hide();}.bind(this));
    }

I pay attention to the line e.stopPropagation(); , without it, the first click(.one) happened immediately after the declaration.
events: {
      'click': function(e) {
        this.countriesDropDownListView.toggleList(e);
      }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question