ng-classify中文文档|ng-classify js中文教程|解析
ng-classify
将 CoffeeScript 类转换为AngularJS模块
编写更少的 JavaScript。少写 CoffeeScript。少写 Angular。
安装
使用npm安装
$ npm install ng-classify
用法
咖啡脚本
ngClassify = require 'ng-classify' content = '''class Home extends Controller constructor: ($log) -> $log.info 'homeController instantiated'''' angularModule = ngClassify content
JavaScript
var ngClassify = ; var content = '\class Home extends Controller\n\ constructor: ($log) ->\n\ $log.info \'homeController instantiated\'\'; var angularModule = ;
吞咽
$ npm install gulp-ng-classify
咕噜声
$ npm install grunt-ng-classify
红宝石
ng_classify – 由pencheck维护
$ gem install ng_classify
早午餐
ng-classify-brunch – 由andrejd维护
$ npm install ng-classify-brunch
目录
概述
AngularJS非常适合利用CoffeeScript 类语法。然而,我们仍然需要处理一些样板代码。ng-classify 解决了这个问题。 注意:所有示例都是有效的 CoffeeScript。
以下是使用 ng-classify 编写控制器的方法
: $scope.coolMethod = someServicecoolMethod
这相当于
angular;
为什么?
取以下典型的 AngularJS 控制器声明(同上)
angular;
那么这有什么问题呢?
angular.module('app').controller
声明中需要
应用程序名称- 有些人通过使用全局变量来避免这种情况
app.controller
,这不利于 JavaScript 卫生
- 有些人通过使用全局变量来避免这种情况
- 参数名称重复,一个用于 getter,
'$scope', 'someService'
一个用于函数参数,function ($scope, someService)
- 根据所需的命名格式,模块类型 (
controller
) 和模块名称 (adminController
) 有重复,由于controller
本示例中的后缀 - 函数是匿名的(未命名的),使得调试更加困难
- 一般冗长
如何?
使用以下语法编写 AngularJS 模块。注意:{{}}
表示占位符
appName extends Animation|Config|Controller|Directive|Factory|Filter|Provider|Run|Service : # module body here
或者
name extends App|Constant|Value : -> return value
CoffeeScript 类
在 AngularJS 中使用 CoffeeScript 类的典型方法如下。
# 203 characters : $scope.coolMethod = someServicecoolMethod angularmodule'app'controller 'adminController''$scope''someService'AdminController
这相当于
// 177 charactersangular;
使用 ng-classify,这就是你所需要的
# 116 characters : $scope.coolMethod = someServicecoolMethod
好处
- 删除不必要的礼仪代码 (
angular.module('app')
) - 编写模块时不需要应用程序名称。它现在是可配置的。
- 通过
constructor
函数只需要一次参数。无需使用数组语法来缩小代码。 - 无需在模块名称后添加模块类型,例如我的Controller、我的Ctrl等。
- 函数命名,调试更方便
- 语法可以说是简洁的。通过消除杂乱,将您的代码带到最前沿。
注意事项
- 为了避免使用全局变量,建议使用
bare: false
CoffeeScript 编译选项。请参阅CoffeeScript 用法
控制器作为语法
AngularJS 提供了两种风格来编写和消费控制器
$scope
this
和Controller as
$scope
例子
: $scope.coolMethod = someServicecoolMethod
$scope
例如查看
Cool It Down!
this
例子
: @coolMethod = someServicecoolMethod
this
例如查看
Cool It Down!
模块类型
应用程序
虽然没有 AngularJS App 模块类型,但为了一致性而包含它。
: -> return 'ngAnimate' 'ngRoute'
相当于
angular;
您可能希望使用then
CoffeeScript 语法通过消除对额外代码行和缩进的需要来更加突出您的代码,如下所示。 注意:这可以用于任何 CoffeeScript 类。
then : -> return 'ngAnimate' 'ngRoute'
注意:应用名称是通过appName选项配置的,而不是类名
动画片
: -> return : # run the animation here and call done when the animation is complete = # this (optional) function will be called when the animation # completes or when the animation is cancelled (the cancelled # flag will be set to true if cancelled).
相当于
angular;
配置
: $routeProvider when '/home' controller: 'homeController' templateUrl: 'home.html' when '/about' controller: 'aboutController' templateUrl: 'about.html' otherwise redirectTo: '/home'
相当于
angular;
持续的
: -> return '401': 'Unauthorized' '403': 'Forbidden' '404': 'Not Found'
相当于
angular;
控制器
下面的例子使用了这个语法
: = userServiceaddUser username
相当于
angular;
指示
: -> return restrict: 'E' transclude: true templateUrl: 'dialog.html'
相当于
angular;
工厂
: return : $loginfo name
相当于
angular;
另一个不错的功能是能够返回类
: return : = -> " "
用法
user = 'Cary''Landholt'fullName = usergetFullName # Cary Landholt
筛选
: -> return "@"
相当于
angular;
提供者
: @name = 'default' = -> name = @name : -> $loginfo name = @name = name
相当于
angular;
跑
: $httpBackendwhenGET/^.*\.$/passThrough
相当于
angular;
服务
: = $loginfo name
相当于
angular;
价值
: -> return name: 'Luke Skywalker' age: 26 name: 'Han Solo' age: 35
相当于
angularvalue'people' name: 'Luke Skywalker' age: 26 name: 'Han Solo' age: 35 ;
多个应用程序
虽然在 AngularJS 应用程序中使用多个应用程序是不必要的,但有些人可能仍然希望这样做。
只需提供应用程序名称作为模块类型的参数。
在下面的示例中,在“通用”应用程序中创建了一个控制器。
'common' : $loginfo 'homeController instantiated'
相当于
angular;
应用程序接口
ngClassify(内容,选项)
内容
所需
类型:String
默认:undefined
可能包含要转换为 AngularJS 模块的 CoffeeScript 类的内容
选项
类型:Object
默认:undefined
选项.appName
类型:String
默认:'app'
AngularJS 应用程序的名称
// for exampleangular
选项.前缀
类型:String
默认:''
为了避免潜在的碰撞,在moduleType前缀可以设置(例如:options.prefix = 'Ng'
)
: $loginfo 'homeController instantiated'
选项.动画
类型:Object
默认:{format: 'spinalCase', prefix: '.'}
options.constant
类型:Object
默认:{format: 'screamingSnakeCase'}
选项.控制器
类型:Object
默认:{format: 'camelCase', suffix: 'Controller'}
选项指令
类型:Object
默认:{format: 'camelCase'}
options.factory
类型:Object
默认:{format: 'upperCamelCase'}
选项过滤器
类型:Object
默认:{format: 'camelCase'}
选项.provider
类型:Object
默认:{format: 'camelCase', suffix: 'Provider'}
选项.服务
类型:Object
默认:{format: 'camelCase', suffix: 'Service'}
选项值
类型:Object
默认:{format: 'camelCase'}
支持的格式
格式 | 例子 |
---|---|
* | 没变化 |
骆驼香烟盒 | 骆驼香烟盒 |
小驼峰 | 小驼峰 |
小写 | 小写 |
尖叫蛇案 | SCREAMING_SNAKE_CASE |
蛇壳 | 蛇案例 |
脊柱病例 | 脊椎病例 |
火车箱 | 火车案例 |
驼峰大写 | 驼峰大写 |
大写 | 大写 |
贡献
变更日志
执照
见许可证