1

i am new to coffeescript. i have already written code in js, now i want to convert it in coffeescript,i have try a lot,i have refer This also. but it won't help me.

here is my code,

this.$scope.callTestFuntion = function(){
    this.blockingObject.render(function(dataURL){
      console.log('via render');
      console.log(dataURL.length);
    });
  }
  this.$scope.blockingObject.callback=function(dataURL){
    console.log('via function');
    console.log(dataURL.length);
    this.myCroppedImage = dataURL;
  }

  var handleFileSelect=function(evt) {
    console.log('here');
    var file=evt.currentTarget.files[0];
    var reader = new FileReader();
    reader.onload = function (evt) {
        this.$scope.$apply(function($scope){
        this.$scope.myImage=evt.target.result;
      });
    };
    reader.readAsDataURL(file);
  };

I want to convert it in coffeescript syntax. please help me.

Thanks

1
  • 1
    Why would you want to convert JS to coffescript, wich eventually/inevitably will be converted back to JS? Commented Feb 11, 2017 at 11:32

2 Answers 2

1

If you just want to convert the code then use http://js2.coffee

@$scope.callTestFuntion = ->
  @blockingObject.render (dataURL) ->
    console.log 'via render'
    console.log dataURL.length
    return
  return

@$scope.blockingObject.callback = (dataURL) ->
  console.log 'via function'
  console.log dataURL.length
  @myCroppedImage = dataURL
  return

handleFileSelect = (evt) ->
  console.log 'here'
  file = evt.currentTarget.files[0]
  reader = new FileReader

  reader.onload = (evt) ->
    @$scope.$apply ($scope) ->
      @$scope.myImage = evt.target.result
      return
    return

  reader.readAsDataURL file
  return

# ---
# generated by js2coffee 2.2.0
Sign up to request clarification or add additional context in comments.

Comments

1

Here is your converted code:

# CoffeeScript code converted from JavaScript
# from Georgi Naumov
# [email protected] for contacts and suggestions
@.$scope.callTestFuntion = ->
  @.blockingObject.render (dataURL) ->
    console.log 'via render'
    console.log dataURL.length
@.$scope.blockingObject.callback = (dataURL) ->
  console.log 'via function'
  console.log dataURL.length
  @.myCroppedImage = dataURL

handleFileSelect = (evt) ->
  console.log 'here'
  file = evt.currentTarget.files[0]
  reader = new FileReader()
  reader.onload = (evt) ->
    @.$scope.$apply ($scope) ->
      @.$scope.myImage = evt.target.result
  reader.readAsDataURL file

And here is resulted JavaScript after compilation:

// Generated by CoffeeScript 1.12.3
(function() {
  var handleFileSelect;

  this.$scope.callTestFuntion = function() {
    return this.blockingObject.render(function(dataURL) {
      console.log('via render');
      return console.log(dataURL.length);
    });
  };

  this.$scope.blockingObject.callback = function(dataURL) {
    console.log('via function');
    console.log(dataURL.length);
    return this.myCroppedImage = dataURL;
  };

  handleFileSelect = function(evt) {
    var file, reader;
    console.log('here');
    file = evt.currentTarget.files[0];
    reader = new FileReader();
    reader.onload = function(evt) {
      return this.$scope.$apply(function($scope) {
        return this.$scope.myImage = evt.target.result;
      });
    };
    return reader.readAsDataURL(file);
  };

}).call(this);

1 Comment

I just fixed one issue with redundant return of function. Good luck :)

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.