3

This is a rather simple problem but I can't spot what the problem is - I think it's to do with not mapping the function correctly on transpilation.

I'm refactoring a function to make it cleaner - here is the original function here:

handleFileSelect(e) {
    e.stopPropagation();
    e.preventDefault();

    var files = e.dataTransfer.files, output = [];

    for (var i = 0, f; f = files[i]; i++) {
      output.push('<li><strong>'+f.name+'</strong> (', f.type || 'n/a', ') - ',
                  f.size, ' bytes, last modified: ',
                  f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                  '</li>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';

    if(this.reader == undefined)
      this.reader = new FileReader();
    this.reader.onload = (e) => {
      document.getElementById('image-chosen')['src'] = e.target['result']; 
    };
    this.reader.readAsDataURL(files[0]);

  }

which works fine. When I do a simple refactor of moving the for loop into its own function it doesn't work and throws this error (after the aforementioned function of course):

buildImageDetails(files, output) {
    for (var i = 0, f; f = files[i]; i++) {
      output.push('<li><strong>'+f.name+'</strong> (', f.type || 'n/a', ') - ',
                  f.size, ' bytes, last modified: ',
                  f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                  '</li>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';

    if(this.reader == undefined)
      this.reader = new FileReader();
    this.reader.onload = (e) => {
      document.getElementById('image-chosen')['src'] = e.target['result']; 
    };
    this.reader.readAsDataURL(files[0]);
  }

Error:

EXCEPTION: TypeError: this.buildImageDetails is not a function
VM124847:84 EXCEPTION: TypeError: this.buildImageDetails is not a function

I'm confused on this because I did the same refactor-esque stuff in the ngOInit but it didn't complain about trying to find the new function.

EDIT Here is where I'm attempting to make use of the function:

handleFileSelect(e) {
  e.stopPropagation();
  e.preventDefault();

  var files = e.dataTransfer.files, output = [];
  this.buildImageDetails(files, output);
}

And this function in itself is called when I drag an image into the dropzone in order to upload it and display it's properties.

EDIT 2

I've been examining this some more have have concluded that the problem is to do with binding. For my events to work correctly I have bound them to the UploadCompnent (the name of the class we are working in) like so:

attachDragListeners() {
    this.cols = document.querySelectorAll('#columns .column');

    [].forEach.call(this.cols, (col) => {
      col.addEventListener('dragstart', this.handleDragStart.bind(UploadComponent), false);
      col.addEventListener('dragenter', this.handleDragEnter.bind(UploadComponent), false);
      col.addEventListener('dragover', this.handleDragOver.bind(UploadComponent), false);
      col.addEventListener('dragleave', this.handleDragLeave.bind(UploadComponent), false);
      col.addEventListener('drop', this.handleDrop.bind(UploadComponent), false);
      col.addEventListener('dragend', this.handleDragEnd.bind(UploadComponent), false);
    });
  }

This function is called in the ngOnInit() to bind the functions to their corresponding events. So essentially the target function that I'm trying to call doesn't exist on the context of the object I'm calling it on.

I'll post back a solution when I find one.

5
  • 4
    I guess this needs more context. How and where do you call buildImageDetails? Commented Jul 18, 2016 at 14:05
  • @GünterZöchbauer sorry - adding it in now Commented Jul 18, 2016 at 14:40
  • 1
    How is handleFileSelect(e) being called? Commented Jul 18, 2016 at 14:44
  • @GünterZöchbauer well i mentioned in the edit that it was being called when I dragged an image into the dropzone Commented Jul 18, 2016 at 15:21
  • I need to see the code. Commented Jul 18, 2016 at 15:22

1 Answer 1

3

I think you need to bind to this not to UploadComponent

this.handleDragStart.bind(this)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I just did this two seconds ago and it worked haha - have a +1 for beating me to the answer. Cheers Gunter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.