2017-02-11 7 views
1

私はcoffeescriptを初めて使用しています。私はすでにjsにコードを書いています。今ではcoffeescriptに変換したいと思っています。私はたくさん試しましたが、Thisも参照しています。しかしそれは私を助けません。ここCoffeeScript関数の問題

は、私は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); 
    }; 

、私のコードです。私を助けてください。

おかげでここ

+1

なぜJSをcoffescriptに変換したいのですか?最終的に/必然的にJSに変換されるのでしょうか? – Thomas

答えて

1

あなただけのコードを変換したい場合は、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 
1

は、変換後のコードは次のとおりです。

# 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 

そして、ここでは、コンパイル後の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); 
+0

これは私のための解決策でもあります+1 –

+0

私はちょうど冗長復帰機能で1つの問題を修正しました。がんばろう :) –