zoukankan      html  css  js  c++  java
  • Get Cordova Ready for Grunt and 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
    

    Install

    Install 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 folder www/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
    

  • 相关阅读:
    javaweb基础----省市县三级联动(javascript版)
    javaweb开发出错排查思路
    javaweb基础----使用原生fileupload上传文件时找不到上传的文件位置
    javaweb基础----Tomcat启动失败(Tomcat9)
    javaweb基础----Tomcat端口被占用
    命令行显示
    java开发环境
    java se ee me 区别
    解决win10 2503 2502 权限等问题
    浏览器只有ie可以登录
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5142349.html
Copyright © 2011-2022 走看看