CoffeeScript and Drupal. Mark Horgan



Similar documents
MODERN WEB APPLICATION DEVELOPMENT WORKFLOW

Art of Code Front-end Web Development Training Program

Getting Started Developing JavaScript Web Apps. this = that. VT Geospatial Forum 2015

Grunt, Gulp & fabs. Build-System and Development-Workflow for modern Web-Applications

IBM Digital Experience. Using Modern Web Development Tools and Technology with IBM Digital Experience

FormAPI, AJAX and Node.js

The Little Real-time Web Development Book

Modern Web Development:

Copyright by Object Computing, Inc. (OCI). All rights reserved. AngularJS Testing

Theming on Drupal 7. Working with Omega s Responsive Framework

What is a stack? Do I need to know?

Visualizing Software Projects in JavaScript

dustin caruso JavaScript / WordPress / UI developer 1230 Parkside Drive South, Reading, PA, USA dustin@dustincaruso.com

Lecture 9 Chrome Extensions

Programming IoT Gateways With macchina.io

Django Assess Managed Nicely Documentation

Example. Represent this as XML

Drupal Performance Tuning

Develop a Native App (ios and Android) for a Drupal Website without Learning Objective-C or Java. Drupaldelphia 2014 By Joe Roberts

MASTERTAG DEVELOPER GUIDE

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

Up and Running with LabVIEW Web Services

Google Docs, Sheets, and Slides: Share and collaborate

The purpose of jquery is to make it much easier to use JavaScript on your website.

Intro to jquery. Web Systems 02/17/2012

Introduction. Inserting Hyperlinks. PowerPoint 2010 Hyperlinks and Action Buttons. About Hyperlinks. Page 1

Visual Logic Instructions and Assignments

Contents Using Jive Anywhere

Personal Profile. Experience. Professional Experience

JavaScript Client Guide

JAVASCRIPT, TESTING, AND DRUPAL

Learning HTML5 Game Programming

CDUfiles User Guide. Chapter 1: Accessing your data with CDUfiles. Sign In. CDUfiles User Guide Page 1. Here are the first steps to using CDUfiles.

Content Author's Reference and Cookbook

Progressive Enhancement With GQuery and GWT. Ray Cromwell

CMSC434 TUTORIAL #3 HTML CSS JavaScript Jquery Ajax + Google AppEngine Mobile WebApp HTML5

Pentesting Web Frameworks (preview of next year's SEC642 update)

Google Web Toolkit. Introduction to GWT Development. Ilkka Rinne & Sampo Savolainen / Spatineo Oy

Lucy Zhang UI Developer Contact:

place/business fetch details, removefromfavorite () function, 189 search button handler bind, B BlackBerry build environment

GrandView. Web Client Software Requirements and Recommendations. Revision

Mike Laurel. Web Developer UI / UX Engineer.

LabVIEW Internet Toolkit User Guide

Google Docs: Share and collaborate

CURRICULLUM VITAE Piotr Kołodziejczyk

Debugging JavaScript and CSS Using Firebug. Harman Goei CSCI 571 1/27/13

Web 2.0 Technology Overview. Lecture 8 GSL Peru 2014

Mobile apps development for Joomla

Google Product. Google Module 1

for Modern Web Design Andy Blanchard / Chris Keller

Sitecore Dashboard User Guide

JavaScript Testing. Beginner's Guide. Liang Yuxian Eugene. Test and debug JavaScript the easy way PUBLISHING MUMBAI BIRMINGHAM. k I I.

BT CONTENT SHOWCASE. JOOMLA EXTENSION User guide Version 2.1. Copyright 2013 Bowthemes Inc.

Data Management Applications with Drupal as Your Framework

Overview. In the beginning. Issues with Client Side Scripting What is JavaScript? Syntax and the Document Object Model Moving forward with JavaScript

How To Customize A Forum On Vanilla Forum On A Pcode (Forum) On A Windows (For Forum) On An Html5 (Forums) On Pcode Or Windows 7 (Forforums) On Your Pc

Web development... the server side (of the force)

Browser tools that make web development easier. Alan Seiden Consulting alanseiden.com

Sharepoint Manual for the Joint Action on Health Workforce Planning and Forecasting

The UC Learning Center: Disabling Pop-Up Blockers

Movie Instructions: Downloading, Saving, & Watching

Improving Magento Front-End Performance

SafeWebApp QuickStart

by Jonathan Kohl and Paul Rogers 40 BETTER SOFTWARE APRIL

Global Preview v.6.0 for Microsoft Dynamics CRM On-premise 2013 and 2015

Dart and Web Components - Scalable, Structured Web Apps

slides at goo.gl/kifue

ACCEPT THE SECURITY CERTIFICATE FOR THE WEB FILTER

Developing multidevice-apps using Apache Cordova and HTML5. Guadalajara Java User Group Guillermo Muñoz Java Developer

THIRD EDITION IN ACTION. Bear Bibeault Yehuda Katz Aurelio De Rosa. FOREWORDS BY Dave Methvin John Resig MANNING

jquery Tutorial for Beginners: Nothing But the Goods

How To Insert Hyperlinks In Powerpoint Powerpoint

The Learn-Verified Full Stack Web Development Program

How To Write A Web Server In Javascript

A set-up guide and general information to help you get the most out of your new theme.

Outlook Web Access Tipsheets

Using Rackspace Webmail

6.170 Tutorial 3 - Ruby Basics

TypeScript for C# developers. Making JavaScript manageable

Learning Web App Development

Front-End Performance Testing and Optimization

Ajax Development with ASP.NET 2.0

BLACKBOARD 9.1: Student Questions and Answers

JavaScript For Cats. An introduction for new programmers. Table of contents. Don't be a scaredy-cat. So easy your human companion could do it too!

Performance Testing for Ajax Applications

8x8 Virtual Office Click2Pop for eagent Setup Guide

Transcription:

and Drupal Mark Horgan DrupalCamp Cork 2013

was inspired by Ruby, Python and Haskell $ -> $(.button ).click -> $.ajax /delete, white space is signticant (Python) type: POST success: (data) -> parentheses are optional (Ruby) alert Success error: (jqxhr) -> alert There was an error deleting your data: #{jqxhr.responsetext (function() { $(function() { return $(.button ).click(function() { return $.ajax( /delete, { type: POST, success: function(data) { return alert( Success ); error: function(jqxhr) { return alert( There was an error deleting your data: + jqxhr.responsetext); ); ); ); ).call(this);

Fits in with existing environment compiles down to so, works with JQuery or any library/platform including Node.js

Your investment in a client-side toolkit can be re-used with different serverside frameworks. Drupal Symfony Rails Django Grails Client-side toolkit (Sass, etc.)

Each file is enclosed in a closure so your variables won t clash with code in other files. (function(){ main.coffee ).call(this);

Variables will always be defined in the local scope. somefunction -> localvariable = 1 var somefunction; somefunction = function() { var localvariable; return localvariable = 1; ; No accidentally defining something in the global scope. var somefunction = function(){ somevariable = 1;

Like Ruby parentheses are optional, but I prefer to include them most of the time, but it s handy to leave them out in callbacks. $(.button ).click -> $.ajax /delete, type: POST success: (data) -> alert( Success ) error: (jqxhr) -> alert( There was an error deleting your data: #{jqxhr.responsetext )

Everything is an expression (at least, as much as possible). Which means the return statement is optional but I prefer to insert them anyway. grade = (student) -> if student.excellentwork A+ else if student.okaystuff if student.triedhard then B else B- else C var grade; grade = function(student) { if (student.excellentwork) { return A+ ; else if (student.okaystuff) { if (student.triedhard) { return B ; else { return B- ; else { return C ; ;

String interpolation numerrors = 2 alert( There are #{numerrors in the form ) var numerrors; numerrors = 2; alert( There are + numerrors + in the form );

Existential operator if obj? console.log( obj exists ) if obj?.property console.log( property of obj exists ) x = a? b x is equal to a if a exists, otherwise it s equal to b x?= defaultvalue if x doesn t exist, set it to defaultvalue

Iterating over arrays for user in users console.log( #{user.firstname #{user.lastname ) and objects for key, value of object console.log( #{key: #{value )

Fat arrow class MessageDialog showmethod: -> $(.closebutton ).click => @dosomethingmethod() dosomethingmethod: -> console.log( Hide the message dialog ) MessageDialog.prototype.showMethod = function() { var _this = this; return $(.closebutton ).click(function() { return _this.dosomethingmethod(); ); ;

Support for classes class Animal constructor: (@type) -> whatami: -> console.log("i am a #{@type") class Dog extends Animal constructor: -> super('dog') class Cat extends Animal -> constructor: -> super('cat') dog = new Dog() dog.whatami() cat = new Cat() cat.whatami()

Criticisms Debugging is an issue Though source maps help Verbally readable!= quicker comprehension Though you can still use && instead of and if (five && six && seven) dostuff(); dosomething() if five and six and seven Significant white-space means will always be compiled. Since you will want to minify the code when serving it to a browser. from Ryan Florence s Blog - bit.ly/isgzmk

Where to learn more? bit.ly/kjijsy bit.ly/ukwyn1 if you subscribe to Safari coffeescript.org a little bit brief coffeescriptcookbook.com has a section on design patterns

Using with Drupal Create a sub-theme from Omega 4 theme. Omega 4 supports Sass, Grunt and Bower. Grunt builds your Sass and Cofffeescript Bower manages your 3rd-party libraries To set up the client-side development environment on OSX see my blog post - bit.ly/19hjqro

To add support for : npm install grunt-contrib-coffee --save-dev And update Gruntfile.js:... watch: {... coffee : { files: [ coffeescript/**/*.coffee ], tasks: [ coffee:dev ] Terminal coffee: { options: { join: true dev: { options : { sourcemap : true files : { js/my-theme-behaviors.js : [ coffeescript/my-theme-behaviors.coffee ] );... grunt.loadnpmtasks( grunt-contrib-coffee );...

The example that comes with Omega 4 will look like this in : do ($ = jquery) -> Drupal.theme::myThemeExampleButton = (path, title) -> $( <a href= #{path title= #{title >#{title</a> ) Drupal.behaviors.myThemeExampleBeahvior = attach: (context, settings) -> $(.some-selector, context).once foo, -> $anchor = Drupal.theme( mythemeexamplebutton, settings.myexamplelinkpath, settings.myexamplelinktitle) $anchor.appendto(this)

To get your Sass and to compile any time you make changes: grunt watch Terminal Grunt supports LiveReload so any changes to Sass or will be updated in the browser without having to press the Refresh button. Google Chrome supports source maps so you can debug in the browser:

Unlike Compass, doesn t minify so you will have to use Uglify to do it:... coffee : { options : { join: true dist : { options : { sourcemap : false files : { js/my-theme-behaviors.normal.js : [ coffeescript/my-theme-behaviors.coffee ] my-theme-behaviors.coffee my-theme-behaviors.normal.js Gruntfile.js my-theme-behaviors.js uglify : { dist : { options : { mangle : true, compress : true files : [{ expand : true, cwd : js, src : [ **/*.normal.js,!**/*.min.js ], dest : js, ext :.js ] );... grunt.registertask( build, [ coffee:dist, uglify:dist ]); minified -.js rather than.min.js so you don t have to change.info file

Dependencies need to be ordered manually: coffee: { options: { Gruntfile.js join: true dev: { options : { sourcemap : true files : { js/my-theme-behaviors.js : [ coffeescript/utils/miscutils.coffee, coffeescript/ my-theme-behaviors.coffee ] There is a library called Coffee Percolator that allows you to specify dependencies at the start of each file but the Grunt plug-in doesn t handle source maps properly. #import utils.miscutils do ($ = jquery) -> Drupal.theme::myThemeExampleButton = (path, title) -> $( <a href= #{path title= #{title >#{title</a> )