Get Cordova ready for grunt and coffeescript

Source: Internet
Author: User



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

Transformwww/js/index.jsTosrc/www/js/index.coffeeIn Example project. Because, we are gotta remote all the Javascript file in folderwww/js/.

Filesrc/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.coffeeFile that tranformed fromwww/js/index.jsWhich is removed already.
  • Gruntfile.coffeeBuild with coffee tasks
  • package.jsonWhich is updated with Modulegrunt-contrib-coffee
Testing
$ grunt coffee$ cordova install android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.