coffeescript v.s. raw javascript 前者节省了无数 标点符号

访问量: 733

前者体积是后者的80% ? (多余的20% 都是 {}, (), ; )
-rw-rw-r-- 1 sg552 sg552 1012 2013-08-05 07:55 js
-rw-rw-r-- 1 sg552 sg552 817 2013-08-05 07:55 coffee

js:

+ app.controller('EditUserController', function($scope, $http,
$routeParams, $location, $route) {
+ return $http.get("/user/" + $routeParams.id).then(function(response) {
+ $scope.user = response.data;
+ $scope.save = function() {
+ return $http({
+ method: 'PUT',
+ url: "/user/" + $routeParams.id,
+ data: $.param({
+ email: $scope.user.email
+ }),
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ }
+ }).then((function(response) {
+ $route.reload();
+ return $().toastmessage('showSuccessToast', '操作成功');
+ }));
- return $scope.applications = response.data;
+ app.controller('EditUserController', function($scope, $http,
$routeParams, $location, $route) {
+ return $http.get("/user/" + $routeParams.id).then(function(response) {
+ $scope.user = response.data;
+ $scope.save = function() {
+ return $http({
+ method: 'PUT',
+ url: "/user/" + $routeParams.id,
+ data: $.param({
+ email: $scope.user.email
+ }),
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ }
+ }).then((function(response) {
+ $route.reload();
+ return $().toastmessage('showSuccessToast', '操作成功');
+ }));
+ };
+ return $scope.destroy = function() {
+ if (confirm("确认删除吗?")) {
+ return $http["delete"]("/user/" +
$routeParams.id).then((function(response) {
+ $location.path('/users');
+ return $().toastmessage('showSuccessToast', '操作成功');
+ }));
+ }
+ };

coffee:


+ ($scope, $http, $routeParams , $location, $route)->
+ $http.get("/user/#{$routeParams.id}").then( (response) ->
+ $scope.user = response.data
+
+ $scope.save = ->
+ $http(
+ method: 'PUT',
+ url : "/user/#{$routeParams.id}",
+ data: $.param(
+ email : $scope.user.email,
+ ),
+ headers: {'Content-Type': 'application/x-www-form-urlencoded'}
+ ).then ( (response) ->
+ $route.reload()
+ $().toastmessage('showSuccessToast', '操作成功')
+ )
+
+ $scope.destroy = ->
+ if(confirm("确认删除吗?"))
+ $http.delete("/user/#{$routeParams.id}").then ( (response) ->
+ $location.path('/users')
+ $().toastmessage('showSuccessToast', '操作成功')
+ )

订阅/RSS Feed

Subscribe

分类/category