2017-02-11 85 views
1

我是新來的咖啡標記。我已經在js中編寫了代碼,現在我想將它轉換爲coffeescript,我已經嘗試了很多,我也參考了This。但它不會幫助我。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的語法。請幫幫我。

感謝

+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

我剛剛解決了一個問題,冗餘返回的功能。祝你好運 :) –