yoeman 简介:http://www.infoq.com/cn/news/2012/09/yeoman

yeoman 官网: http://yeoman.io/

yeoman 是快速创建骨架应用程序的WEB前端工具,实际由 yo, grunt, bower 三个组建而成。

  • Yo scaffolds out a new application, writing your Grunt configuration and pulling in relevant Grunt tasks that you might need for your build.

  • Grunt is used to build, preview and test your project, thanks to help from tasks curated by the Yeoman team and grunt-contrib.

  • Bower is used for dependency management, so that you no longer have to manually download and manage your scripts.

简单来说就是 Yo 是配置 Grunt 的 task 的工具,Grunt 是一个部署、预览和测试前端项目的工具,而 Bower 是一个包的管理工具,可以下载 jquery, backbone 等库。

PS: 要运行 yeoman 首先需要配置环境,配置好 node.js 、git 环境。

下面开始写一个 todo-list demo 作为例子

一、首先安装 yo,这会自动安装 grunt, bower

npm install -g yonpm install -g generator-webapp 

PS: yo 是命令开头,webapp 是 另外一个参数. yo 可以生成其他各种应用配置。如 backbone, angularjs等,前提首先安装 generator-backbone,即

yo webapp

此时可以看到

webapp 默认带有 Sass 版的 bootstrap库,等到下载完毕后可以看到以下文件:

app 为项目主目录,是部署的根目录

node_modules 为 nodejs的包文件

test 为测试目录,专门用于单元测试,用的是 mocha 来测试

Gruntfile.js 是配置 grunt 自动化任务的配置文件,具体配置可以参考下 Grunt官网

二、添加JS库,这里添加 backbone,bower 会自动下载 underscore.

bower install backbone

三、运行

grunt server

它将运行应用,并监控文件的变化,一旦有改变,就会自动刷新浏览器。

文件又这几个,用的是

  • HTML5 Boilerplate 模板(默认)
  • RequireJS (可选)

四、开始写应用

index.html

 <!doctype html>
 <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
 <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
 <!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
 <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
     <head>
         <meta charset="utf-8">
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
         <title></title>
         <meta name="description" content="">
         <meta name="viewport" content="width=device-width">
         <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
         <!-- build:css(.tmp) styles/main.css -->
         <link rel="stylesheet" href="styles/bootstrap-2.3.2.min.css">
         <link rel="stylesheet" href="styles/main.css">
         <!-- endbuild -->
         <!-- build:js scripts/vendor/modernizr.js -->
         <script src="bower_components/modernizr/modernizr.js"></script>
         <!-- endbuild -->
     </head>
     <body>
         <div class="container">
              <div class="todo-container">
                 <div class="header">
                     <h2>Todo List</h2>
                     <div class="todo-input">
                         <div class="controls">
                             <input type="text" placeholder="Enter to save" class="todo-input-text" autofocus>
                         </div>
                     </div>
                 </div>
                 <div class="content">
                     <div class="todo-item-list todo-undone-list clearfix">
                         <p>Things to do:</p>
                     </div>
                     <div class="todo-item-list todo-done-list clearfix">
                         <p>Done Things:</p>
                     </div>
                 </div>
                 <div class="footer">
                 </div>
             </div>
         </div>

         <script type="text/template" id="todo-input-template">
         </script>
         <script type="text/template" id="todo-item-template">
             <span class="order"><%= order%></span>
             <a href="javascript:void(0);" class="done-<%= done%>">
                 <b><%= title%></b>
                 <button title="删除" class="btn btn-mini pull-right todo-item-remove"><i class="icon-remove"></i></button>
                 <% if (done) { %>
                     <button title="撤销" class="btn btn-mini pull-right todo-item-undone"><i class="icon-repeat"></i></button>
                 <% } else { %>
                     <button title="完成" class="btn btn-mini pull-right todo-item-done"><i class="icon-ok"></i></button>
                 <% } %>
             </a>
         </script>
         <!--[if lt IE 7]>
             <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
         <![endif]-->

         <!-- build:js scripts/main.js -->
         <script data-main="scripts/main" src="bower_components/requirejs/require.js"></script>
         <!-- endbuild -->
 </body>
 </html>

main.coffee

 require.config
   baseUrl : './scripts/'
   shim :
     underscore :
       exports : '_'
     backbone :
       deps : [
         'underscore'
         'jquery'
       ]
       exports : 'Backbone'
     backboneLocalStorage :
       deps : [
         'backbone'
       ]
     app :
       deps : [
         'underscore'
         'jquery'
         'backbone'
       ]

   paths :
     jquery : '../bower_components/jquery/jquery'
     underscore : '../bower_components/underscore/underscore'
     backbone : '../bower_components/backbone/backbone'
     backboneLocalStorage: '../bower_components/backbone/examples/backbone.localStorage'

 require ['app'], (app) ->
   app.init()

app.coffee

 define 'app', ['jquery', 'underscore', 'backbone', 'backboneLocalStorage'], ($, _, Backbone) ->

   TodoModel = Backbone.Model.extend
     defaults : () ->
       title : 'Untitle',
       done  : false,
       order : Todos.length+1

     initialize : () ->
       this.save()

   TodoList = Backbone.Collection.extend
     model : TodoModel
     localStorage : new Backbone.LocalStorage 'todos-backbone'
     done : () ->
       this.where done : true

   Todos = new TodoList  # todo 集合

   TodoView = Backbone.View.extend
     tagName  : 'div'  # 外容器
     template : _.template $('#todo-item-template').html()   # 模板HTML

     # 初始化,坚听对象
     initialize : () ->
       this.listenTo this.model, 'change',  this.render
       this.listenTo this.model, 'destroy', this.remove

     # 事件绑定
     events :
       'click  button.todo-item-done'   : 'done'
       'click  button.todo-item-remove' : 'clear'
       'click  button.todo-item-undone' : 'undone'

     done : () ->
       if this.model.get('done') == false  # 本身是未完成状态的
         this.model.set done : true
         this.remove()

     undone : () ->
       if this.model.get('done') == true  # 本身是完成状态的
         this.model.set done : false
         this.remove()

     clear : () ->
       this.model.destroy()

     render : () ->
       this.$el.html this.template this.model.toJSON()
       return this

   AppView = Backbone.View.extend
     # 初始化保存DOM对象
     initialize : () ->
       this.$input = this.$('.todo-input-text').focus()
       this.$todoList = this.$('.todo-undone-list')
       this.$todoDoneList = this.$('.todo-done-list')

       this.listenTo Todos, 'add',         this.addOne
       this.listenTo Todos, 'change:done', this.addOne

       Todos.fetch()

     events :
       'keypress input.todo-input-text'    : 'createOnEnter'

     # Enter 时保存
     createOnEnter : (e) ->
       if e.keyCode != 13
         return
       if !this.$input.val()
         return;

       Todos.create title: this.$input.val()
       this.$input.val('')

     addOne : (todo) ->
       view = new TodoView  model : todo
       if todo.get('done')
         # 已经完成的加入已完成列表
         this.$todoDoneList.append(view.render().el);
       else
         # 未完成的加入未完成列表
         this.$todoList.append(view.render().el);   

       # Todos.each (todo) ->
       todo.save() 

   App = new AppView el : $('.todo-container')   # 主应用UI

   return init : () ->
     Backbone.history.start()
     

main.css

 .todo-container {
     margin: 50px auto 0 auto;
     width: 300px;
 }
 .todo-item-list {
     margin-top: 20px;
 }
 .todo-item-list > div {
     float: left;
     width: 90%;
     margin-top: 10px;
     padding-left: 5%;
 }
 .todo-item-list > div a:hover {
     text-decoration: none;
 }
 .todo-item-list > div button {
     margin: 0 3px;
 }
 .todo-item-list > div span.order {
     margin-right: 10px;
     color: #B6B6B6;
     font-style: italic;
 }
 .todo-item-list > p {
     font-weight: bold;
     margin-top: 1em;
 }

效果图

补充:

这里用的是 coffeescript,yeoman会自动转成js。关于todo-list,可以上官网参考下,我这里简化了部分代码。

最新文章

  1. java中IO流小解
  2. WinForm------GridControl添加底部合计框
  3. No matching provisioning profile found: Your build settings specify a provis...
  4. ORACLE口令管理
  5. 电子现金、电子钱包、qPBOC、闪付、UPCash
  6. tomcat错误:@HandlesTypes annotation of one or more ServletContentInitializers
  7. Python:Python 3.x 的革新
  8. c++builder 重载WindowProc、WndProc 截获消息
  9. Abel 分部求和法
  10. hadoop2——新MapReduces——yarm详解
  11. android应用的不同版本间兼容性处理
  12. 我的vimrc配置
  13. 解读sample4
  14. malloc 申请得到的内存后,再 free 释放它的时候,操作系统会立即收回那块内存吗?
  15. js open() 与showModalDialog()方法
  16. grub4dos新手指南-2
  17. XBOX360 硬盘玩游戏
  18. linux之软件安装
  19. ASP.NET Core 2 学习笔记(一)
  20. CMDB服务器管理系统【s5day91】:如何实现允许临时修改主机名

热门文章

  1. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
  2. Linux 开机时网络自动连接
  3. Python的单元测试(一)
  4. Android性能优化之巧用软引用与弱引用优化内存使用
  5. DDD 领域驱动设计-两个实体的碰撞火花
  6. 【夯实PHP基础】nginx php-fpm 输出php错误日志
  7. 编译器开发系列--Ocelot语言6.静态类型检查
  8. OpenSUSE下编译安装OpenFoam
  9. 递归实现n(经典的8皇后问题)皇后的问题
  10. struts2国际化