繁体   English   中英

CoffeeScript函数问题

[英]CoffeeScript function issue

我是Coffeescript的新手。 我已经用js编写了代码,现在我想将其转换为coffeescript ,我做了很多尝试,我也参考了 但这对我没有帮助。

这是我的代码,

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);
  };

我想将其转换为coffeescript语法。 请帮我。

谢谢

这是您转换后的代码:

# CoffeeScript code converted from JavaScript
# from Georgi Naumov
# gonaumov@gmail.com 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

这是编译后的结果JavaScript:

// 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);

如果您只想转换代码,请使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM