Get Cordova Ready for Grunt and CoffeeScript

來源:互聯網
上載者:User

標籤:cordova   grunt   coffeescript   


Cordova, Grunt and Coffee

You may reference to below if you deside to work with coffee instead of Javascript in Cordova project.

Prepare Cordova Helloworld Project

This guide is based on Hello World project which is generated by the following commands.

$ cordova create hello com.example.hello HelloWorld$ cd hello$ cordova platform add android
InstallInstall coffee Command
$ npm install -g coffee
Install grunt-contrib-coffee Grunt Plugin
$ npm install grunt-contrib-coffee --save-dev
Generate Working Tree

Transform www/js/index.js to src/www/js/index.coffee in example project. Because, we are gotta remote all the javascript file in folderwww/js/.

File src/www/js/index.coffee:

app =     initialize: () -> this.bindEvents()    bindEvents: () ->        document.addEventListener ‘deviceready‘, this.onDeviceReady, false    onDeviceReady: () ->        app.receivedEvent ‘deviceready‘    receivedEvent: (id) ->        parentElement = document.getElementById id        listeningElement = parentElement.querySelector ‘.listening‘        receivedElement = parentElement.querySelector ‘.received‘        listeningElement.setAttribute ‘style‘, ‘display:none;‘        receivedElement.setAttribute ‘style‘, ‘display:block;‘        console.log ‘Received Event: ‘ + id
Update Gruntfile.coffee
module.exports = (grunt) ->    grunt.initConfig        pkg: grunt.file.readJSON ‘package.json‘        coffee:            options:                bare: true            build:                expand: true                cwd: ‘src/www‘                src: [‘**/*.coffee‘]                dest: ‘www‘                ext: ‘.js‘                extDot: ‘first‘    grunt.loadNpmTasks ‘grunt-contrib-coffee‘
Now We Have
  • src/ folder as coffee scripts location
  • src/www/js/index.coffee file that tranformed from www/js/index.js which is removed already.
  • Gruntfile.coffee build with coffee tasks
  • package.json which is updated with module grunt-contrib-coffee
Testing
$ grunt coffee$ cordova install android

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.