Showing posts with label Jade. Show all posts
Showing posts with label Jade. Show all posts
Tuesday, October 6, 2015
Do we need HTML and CSS when we got JavaScript or more? A case for AbsurdJS, Meteor, Famo.us and CoffeeScript.
Introduction
AbsurdJS is an amazing project. What is it about? A processor for your HTML and CSS in JavaScript. In JSON to be more precise.The problem
CSS was invented for designers. But do you really know that much designers that are able to provide you with a nice CSS rules that will be awesome on all screens? You are favored by gods if it is your case. Most of the time, they will provide you with some PSD (what an ugly form of design), AI (if you are lucky), Sketch or SVG (here, it is not anymore based on luck, it is like having an angel upon your shoulders).The same goes with HTML. Semantics that appears with HTML5 were added for SEO. But do you know that much SEO guys that will give you the right HTML code? I haven't seen any in my whole 20 years on the web carrier(though SEO is not that long of a job).
Thus, why are we using 3 languages for writing our codes. Is that right? Do we have to? Is it done for helping us and keeping separation of concerns? Funny. Patterns like this exists since the dawn of design patterns from the GoF. And what if we need to synchronize this HTML, CSS and JS back together for animations purposes? We request back in our code the HTML tags that we have just declared? We hooks on the CSS? If we need a color theme, do we have to write it twice or more? Funny, too. Even media queries for responsive web designs, seems like an odd way of reproducing the abstract factory pattern (or the bridge depending on if you know to who you are distributing your files).
The solutions
There are many. Generally, I was keen on using preprocessors: SASS, LESS, Stylus, YAML, Jade, Blade, CoffeeScript, Typescript, Dart, ... But, it finally does not give me a proper answer to the problems previously mentioned. It still need glue code to make all of this work together properly. Things started to shape up when I end up using Meteor, Famo.us and Velocity.js. The Blaze component of Meteor is transforming your HTML, more precisely, your HTML templates using Spacebar, a derivative of Mustache, into JS code. The same goes with Famo.us. It creates HTML and CSS rules that plays well together so that they limit layout trashing issues and combines this technics with advanced physic and animation engine. The same goes with Velocity.js. This jQuery plugins brings back synchronized CSS animations in with JS.Still, plain vanilla JavaScript is a pain to write. While there are plenty of transpilers that could lessen the burden (like Dart and Typescript, to mention the most used ones), none have proven to be as easy as CoffeeScript to me.
Conclusion
Now the solution starts to shape up. Why not use the power of AbsurdJS, Meteor, Famo.us and CoffeeScript? This combination would be a real full stack. It would goes from the persistance layer, the application server to the front end with one and only one language. A transpiled one that even lessen the number of lines that you need to write.
Labels:
CoffeeScript,
Innovation,
Jade,
Meteor,
Meteor.js,
SEO,
WPO
Responsive SVG devices for famo.us and Meteor
Introduction
When creating a landing page or when demonstrating an app, there are often some smartphone, tablet or desktop screenshots. This pattern of demonstration could be a bit enhanced if instead of simple screenshots, the real app could be demonstrated. That would be the power of HTML5 sets in motion.The base component
Famo.us has a nice component that could be leverage for that: the ContainerSurface. It is basically a small context that can be clipped. By living in the same space of your main web site, it allows interesting demonstration patterns where you could demonstrate your app. The problem is to display this context exactly on the device that you want to present.By using an adaptive or a responsive SVG, you can extract coordinates of the device screen where you want to present your app. You can then instantiates a ContainerSurface on this same coordinates and put you app as its content.
In this little plugin fview-devices for famous-views, a Meteor package, this is exactly what is done. Here is a little demonstration of it:
You can also play with it in this live demo: fview-devices.
Conclusion
Beyond this little example, some interesting points emerge such as the capability to mix SVG and Famo.us. It paves the way to further enhancements. For instance, CSS Transform and Animation are possible in SVG. Unfortunately, they are not hardware accelerated. Worst, some browsers have very uneasy bugs to circumvent. By extracting portions of SVG and putting them into Famo.us surfaces, you regain hardware acceleration and circumvent issues from wrong implementations. Basically, you open up wider the capability to use SVG to the mobile platforms.It seems to me that a new story of apps using SVG, Famo.us and Meteor is about to begin. Happy coding.
Labels:
CoffeeScript,
Famo.us,
Innovation,
Jade,
Meteor,
Meteor.js,
SVG,
WPO
Famo.us dot loader animation using Meteor's Famous-Views
A simple customizable dot loader ported for famous-views:
Source code: fview-dotloader
Demo site: fview-dotloader
Original work: LeXXik and Talves.
Read more »
Source code: fview-dotloader
Demo site: fview-dotloader
Original work: LeXXik and Talves.
Labels:
CoffeeScript,
Famo.us,
Innovation,
Jade,
Meteor,
Meteor.js
Setup a Famo.us experiment using Codepen.io
I've created a CodePen for easing creation of experiments with Famo.us. It uses Jade and CoffeeScript as its defaults and imports Famo.us and jQuery in their minified version. Fork it and play with it. Happy coding!
Read more »
Labels:
CodePen,
CoffeeScript,
Famo.us,
Innovation,
Jade
Location:
Paris, France
Jade within Atom.io: Fasten your HTML and SVG authoring and template debugging
When writing HTML, XML, SVG or templates based on this language, it is cumbersome to open and close tags. Even with a good editor that closes tags for you or a good plugin that write snippet codes, you end up with unreadable and long source files. Jade removes this hassle by providing a simple and elegant code that transpiles to tagged code.
Here's an example of Jade transpiled to HTML:
However, it is sometime handy to seen what your generated code will look like. I've published another Atom.io plugin that preview the generated results with ctrl+alt+j.
The package is available on Atom.io's repository: https://atom.io/packages/jade-compile
And its code source is available on Github: https://github.com/PEM--/jade-compile
Read more »
Here's an example of Jade transpiled to HTML:
However, it is sometime handy to seen what your generated code will look like. I've published another Atom.io plugin that preview the generated results with ctrl+alt+j.
The package is available on Atom.io's repository: https://atom.io/packages/jade-compile
And its code source is available on Github: https://github.com/PEM--/jade-compile
Create your slide deck using Famo.us and Meteor
Introduction
Yesterday, there was the Paris Famo.us Meetup organized by AdFab and Meetic France. I was presenting the first part, which consists of a live coding or demo showcasing. I intended to demonstrate some basic techniques on Famo.us's physic engine. Live coding is tough. There are always some "demo effect" and time runs out very quickly.Fortunately, for this demo preparation, I created some slides. As the meetup was about Famo.us, I decided to code my slide deck using... Famo.us and Meteor.
The slides are deployed on Meteor: MeetupFamousSlides.
The core components
If you think about a presentation tool, it only consists of static pages that you present one after the other. This is definitely the job of iron:router.Creating each slide is easily done when you are using a templating system with emphasis on semantic. Jade and Markdown fit very well in this equation. Therefore, I ended up choosing mquandalle:jade. Each slide is a single Jade file. Thanks to the power of Meteor, you can write additional logic in the template for animating the slide's content.
Speaking of animations, modern slide deck use some of them. Famo.us is really a nice framework for this task. But coupling Meteor and Famo.us is a bit tedious. This is where enters gadiccohen:famous-views. It bridges both worlds with an incredible ease of use. I strongly recommend you to have an eye on it.
Conclusion and code reuse
In less than 2 hours, the presentation engine was ready and I had a basic presentation. Adding slides was a no brainer simply consisting in updating the router and adding a Jade file.You can find the code of the engine and the slide deck on this Github repository.
Labels:
CoffeeScript,
Famo.us,
Innovation,
Jade,
Markdown,
Meteor,
Meteor.js
Location:
Paris, France
The fastest loading screen for Meteor
Introduction
Meteor offers a great way to build web apps at an incomparable speed. It leverages the power of Javascript anywhere. In production, its powerful server side build system named Isobuild transpiles all your HTML files into JS files, thanks to Blaze, and packs them with your JS files. This operation leads to a single JS file loaded by a tiny initial HTML loader that bootstraps your app.Depending on your development strategy, this single JS file can end up very fat. This could seriously annoy your first time users who will wait many seconds before seing something on their screen. Of course, proxying your servers with Gzip or SPDY capable fronts greatly helps lowering this initial loading. Truncating this JS file could also help. Using a multiple domains strategy and CDNs also offer great loading time cuts. Still, on mobile, with a bad network access like GPRS, these several seconds rapidly become a problem. To understand the ins and outs of web performance optimization, WPO, I strongly recommend reading the book High Performance Browser Networking by Ilya Grigorik.
What is this about?
Not to be confused with the great masterpiece fast render from Arunoda Susiripala which is used for fastening the initial data required from your collections, the technic that I expose hereafter tackles the initial page load time, PLT. The first paints on the screen, if you prefer. This first paint comes even before the tiny initial HTML loader is fully loaded by your users's terminal.On a regular Meteor app, while your users connect to your site and till the initial load of the JS file, their browser remains blank. Of course, they could see that something is happening. Almost all browser display at least a progress indicator. But users may not notice it and think that your site is either dead or hosted on a distant other planet. That is just very bad for user acquisition, isn't it? If you want to better understand how this affects user's perceptions as well as your search engine optimization strategy, SEO, Paul Irish has covered this in great details in his incredibly informative talk: Fast enough (check the comments for the slides and the Youtube video). What you need to know is that your PLT will be nice if your initial screen painting is under 14k. That seems like an impossible goal to achieve with an out-of-the-box Meteor app with an average weight of ~50k of compressed initial JS file.
But Meteor is far from being a classic website approach and is definitely not a low end build tool. You can use the raw power of its Isobuild to ask your servers for an injection of your initial screen content directly within the tiny initial HTML loader. It is the key to unlock the 14k barrier.
Performing the injection
This could be a bit scary for the majority of Meteor devs. But once again, Arunoda has you well covered. By using another great masterpiece from Arunoda and Gadi Cohen, this injection process is a no brainer: inject initial. Let us jump in the code.In your app, installing inject initial is the one step classical stance:
meteor add meteorhacks:inject-initialFor the sake of brevity, the codes hereafter are in CoffeeScript.
In your server code, you can perform the initial injection. Hereafter, I'm just putting a little CSS spinner. Nothing fancy.
/server/startup.coffee
Inject.rawHead 'loader-style',Here we have somewhat altered, the DOM structure of our app. Indeed, we have just created a body while the head of your your initial HTML loader wasn't even finished to get build. Is that a big problem? No. Your server will carry on filling the rest of your app in the body section that you have created (the styles, the scripts, ...).
# Force the initial scale for Android and iOS as our spinner may be
# distorted by their default viewport values.
'<meta name="viewport" content="width=device-width,maximum-scale=1,' +
'initial-scale=1,user-scalable=no">' +
# The loading spinner needs some theming.
'<style>' +
'html{background-color: #36342e;}' +
'body{color:#ddd;overflow:hidden;width:100%;}' +
'.spinner {' +
'bottom:0;height:80px;left:0;margin:auto;position:absolute;' +
'top:0;right:0;width:80px;' +
'-webkit-animation: rotation .6s infinite linear;' +
'animation: rotation .6s infinite linear;' +
'border-left:6px solid rgba(255,194,0,.20);' +
'border-right:6px solid rgba(255,194,0,.20);' +
'border-bottom:6px solid rgba(255,194,0,.20);' +
'border-top:6px solid rgba(255,194,0,.9);' +
'border-radius:100%;' +
'}' +
'@-webkit-keyframes rotation {' +
'from {-webkit-transform: rotate(0deg);}' +
'to {-webkit-transform: rotate(359deg);}' +
'}' +
'@-moz-keyframes rotation {' +
'from {-moz-transform: rotate(0deg);}' +
'to {-moz-transform: rotate(359deg);}' +
'}' +
'@-o-keyframes rotation {' +
'from {-o-transform: rotate(0deg);}' +
'to {-o-transform: rotate(359deg);}' +
'}' +
'@keyframes rotation {' +
'from {transform: rotate(0deg);}' +
'to {transform: rotate(359deg);}' +
'}' +
'</style>'
# The loading spinner is a CSS animation.
# /!\ WARNING: The trick is to create a fake body by injecting data
# in the HTML's head as Meteor is requesting JS file in a blocking
# fashion and mobile only allow 1 HTTP request at a time on a GPRS network.
Inject.rawHead 'loader-body2', '<body><div class="spinner"></div></body>'
At this point, you could tell me: "But wait? There is now a title and meta tags in the body of my app. That's just a disgusting code.". You are right. But the browsers are really not impacted by this. I could reply back: "Who care?". But, indeed, I care. Simply because one of the biggest interest of web technology is that all your codes are readable. It is important to let other developers having the possibility to understand how stuff works. It is part of the learning legacy that we owe to the web. So let us put things in the correct order.
Cleaning our loaded codes is done directly on the client side. We do that once everything is loaded. No hurry as it has no impact except a good readability of our DOM structure.
/client/startup.coffee
Meteor.startup ->That's it. Nice and clean.
# Reshape DOM: put back title and meta elements in the head.
# style and script tags can leave in the body tag.
$head = $ 'head'
for tag in ['meta', 'title']
$tags = $ tag
$head.append $tags.clone()
$tags.remove()
Testing it
There are several ways of testing these results and their behavior. On Mac, I use Network link conditionner, a handy tool provided by Apple. It allows you to modify the behavior of you TCP stack and use GPRS, 3G, ... profiles. Very neat when you are testing hybrid or native apps.If you are testing full web apps, I strongly recommend using Chrome DevTools. The mobile emulator as a special feature allowing to change the network throttling.
Some constraints you need to be aware of
When your initial HTML loader and your initial JS file are being loaded, the JS engine and the SVG rendering engine are paused. I've always been annoyed by this browser behavior but we have to live with it. Thus, you can't put JS loaders or SVG animated loaders. You should favor CSS loader and choose the one that will be the less CPU intensive. Here, you should favor CSS3 animated loader which are animated by the GPU as the CPU will be intensively interpreting what it is finishing pumping from you servers.A side note to finish this little article. This technic is not here to bypass the necessary work of making your app as lightweight as possible. It is still a good practice to ensure that your app will avoid consuming the data subscription of your users ;-)
Location:
Paris, France
Monday, October 5, 2015
Meteor walktrough: Samples from JSConf 2013
Introduction
Meteor is a full stack JS framework: JS is used on the server side as well as on the client side. This allows a code mutualisation between this generally separated world. Meteor really shines in its way of handling this mutualisation. It allows you to specify where your code will be used. It relies on a Node and MongoDB.In October 2013, Emily Stark, one of the core developper behind Meteor, has presented at the Asia JSConf 2013 a nice tutorial. I simply provides hereafter the transformed files in CoffeeScript, Jade and Stylus that will help you out understanding and playing with it while watching the associated video:
Settings
First, you need a running installation of MongoDB and Node. On OSX, it is done using Homebrew:brew install mongodb nodeNow, it is time to import Meteor:
curl https://install.meteor.com/ | shNote: The regular installation through NPM does not import the sample application for the generators. Therefore, I recommend using the regular automated shell script.
Meteor comes with an handy package management named Meteorite and its official repository Atmosphere. In essence, its purpose is to handle all packages that merges the world of the server side, NPM, and the world of the client, Bower. It is installed as regular Node package:
npm install -g meteorite
Preparing the playground
Now create a sample app:mrt create JsConfAsia
cd JsConfAsia
Remove unused package and add some nice other ones:
mrt remove autopublishJust remove the created file, that we are going to recreate:
mrt add meteor-jade
mrt add meteor-coffeescript
mrt add meteor-bootstrap-3
mrt add meteor-stylus
rm JsConfAsia.*
Replacing the files
Now, in order to better follow the nice video, I propose you this set of simple file:JsConfAsia.stylbody
font-family: Helvetica, Arial, sans-serif
p
margin: 0
padding: 0
li
padding-bottom: 5pxJsConfAsia.jade.htmlhead
title JsConfAsia
body
.container
+header
+talksList
template(name='header')
h2 Judge jsconf.asia talks
h3 Be ruthless
template(name='talksList')
ul
each talks
li
p: <strong>#{title}</strong> by #{speaker} (#{votes} votes)
.btn-group.btn-group-sm
button.btn.btn-success.upvote
<span class="glyphicon glyphicon-thumbs-up"></span> +1
button.btn.btn-danger.downvote
<span class="glyphicon glyphicon-thumbs-down"></span> -1JsConfAsia.jade.htmlTalks = new Meteor.Collection 'talks'
Meteor.methods
upvote: (talkId) ->
talk = Talks.findOne talkId
Talks.update talkId, {$set: {votes: talk.votes + 1}}
downvote: (talkId) ->
talk = Talks.findOnedOne talkId
if talk.votes isnt 0
Talks.update talkId, {$set: {votes: talk.votes - 1}}
if Meteor.isServer
Meteor.publish 'talks', ->
Talks.find()
else if Meteor.isClient
Meteor.subscribe 'talks'
Template.talksList.talks = ->
Talks.find {}, {sort: {votes: -1}}
Template.talksList.events
'click .upvote': ->
Meteor.call 'upvote', @_id
'click .downvote': ->
Meteor.call 'downvote', @_id
Adding some sample data into MongoDB
Meteor eases the use of MongoDB shell with this little command:
mrt mongoNow, simply cut and paste the following value:
db.talks.insert({title: "Clone Toy Story in 24h with CSS", speaker: "Lea Verou", votes: 0});
db.talks.insert({title: "Enhance! Enhance!", speaker: "That Adobe guy", votes: 0});
db.talks.insert({title: "Templates: the child of Satan", speaker: "Peter Hunt", votes: 0});
To exit and come back to your shell CLI, hit the classic key stroke: ctrl+c.Execute Meteor
Just execute the basic command:
mrtAnd open your browser of choice on http://localhost:3000.
This should provide the following reactive and multi-users web app in less than 100 lines of codes:
Classy!
Subscribe to:
Posts (Atom)


