Tuesday, November 15, 2016

Write Reusable JavaScript Business Logic with peasy-js

When writing applications, we often couple our valuable business logic with framework specific code. For example, when using Angular it is common to disperse business logic across services, controllers, and even directives.

This also applies to JavaScript written for the back-end, where it common to litter our Sails (for example) controller actions with business logic that directly consumes our data access logic via ORM/ODMs such as Mongoose, and other cross-cutting concerns.

This coupling generally leads to code that is difficult to reuse, scale, test, and adopt or migrate to new technologies.

In this article, I'll show you how to use the peasy-js library to help structure your business logic in a way that makes it highly re-usable between the front and back-end parts of your app, and easily portable between different frameworks.

Disclosure: I am the author of peasy-js

Should We Stop Using Frameworks?

On the contrary, I believe these frameworks offer tremendous benefit, both on the client and server. What I am proposing, however, is to abstract our business logic into composable units by creating code that is completely agnostic of its consumers.

By componentizing our business logic, we can easily test, swap out, rearrange, reuse, and consume these components within any application architecture, using any JavaScript client, server, data access technologies, and framework imaginable.

Separate Your Business Logic

peasy-js is a middle-tier framework that makes it trivial to whimsically swap out UI, back-end, and data access frameworks in our applications by creating business logic in a composable, reusable, scalable, and testable manner. In other words, peasy-js offers guidance in abstracting our business logic into composable units by authoring code that adheres to separation of concerns (SoC).

Framework Fatigue

Wait, don't go yet!

I know what you're thinking, "ugh, another framework?". Yes, peasy-js is indeed a microframework. However, chances are if we venture down the path of componentizing our business logic, we'll end up writing our own micro framework anyhow.

Countless hours have been contributed to the design, development, and testing of peasy-js, supporting almost any workflow imaginable. With a low barrier to entry, I'm hopeful that you'll find the small investment in learning to be well worth your time.

If however, you find that peasy-js isn't quite for you, hopefully you'll gain some insight into how you can implement your own business layer using some of the patterns in the framework.

The Main Concepts

Let's check out what peasy-js offers us:

  • Easy to use and flexible business and validation rules engine
  • Scalability and reusability (decouples business and validation logic from consuming code and frameworks)
  • Easy testability

peasy-js encompasses four main concepts. Each one is outlined below with a brief description and will be covered in more depth throughout the article.

BusinessService

A BusinessService implementation represents an entity (e.g. users, or projects) and is responsible for exposing business functionality via commands. These commands encapsulate CRUD and other business related functions.

Command

The Command is responsible for orchestrating the execution of initialization logic, validation and business rule execution, and other logic (data proxy invocations, workflow logic, etc.), respectively, via the command execution pipeline.

Rule

A Rule can be created to represent a validation rule (field length or required) or a business rule (authorization, price validity, etc.). Rules are consumed by commands and can be chained, configured to execute based on a previous rule's execution, etc. Rules can also be configured to run code based on the result of their execution.

DataProxy

The DataProxy is responsible for data storage and retrieval, and serves as an abstraction layer for data stores that encompass (but are not limited to) the following:

  • Relational Databases - SQLite, MySQL, Oracle, SQL Server, etc.
  • Document (NoSQL) Databases - MongoDB, VelocityDB, etc.
  • Services - HTTP, SOAP, etc.
  • Cache Stores - Redis, Azure, etc.
  • Queues - RabbitMQ, MSMQ, etc.
  • File System
  • In-memory data stores for testing

Examples: Peasy-js in Action

Note: A simple browser example can be viewed on plnkr that covers everything discussed in this section.

Here is a sample of what it might look like to consume business logic written with peasy-js within an Angular service on the client:

Continue reading %Write Reusable JavaScript Business Logic with peasy-js%


by Aaron Hanusa via SitePoint

TextComplete – jQuery Plugin to Create Autocomplete for Textarea

jQuery.textcomplete is a jQuery plugin that introduces autocompleting power to textareas, like a GitHub comment form has.


by via jQuery-Plugins.net RSS Feed

20+ Docs and Guides for Front-end Developers (No. 10)

It’s that time again to get learning! As before, I’ve collected a number of different learning resources, including guides, docs, and other useful websites to help you get up to speed in different areas of front-end development.

So please enjoy the tenth installment of our Docs and Guides series and don’t forget to let me know in the comments of any others that I haven’t yet included.

1. CSS Mod Queries and Range Selectors

You’ll understand this one better if you read Patrick Clancey’s recent article on A List Apart on “mod queries”. This is a tool/guide that helps you understand these CSS selectors and how they can be used to lay out unknown numbers of items in grid layouts.

CSS Mod Queries and Range Selectors

2. You Might Not Need JavaScript

With the smart warning that these components might not be semantic or accessible to all users, this site has collected a bunch of different techniques that demonstrate what can be done with just HTML, CSS, Sass.

You Might Not Need JavaScript

3. The HTML5 Mega Cheat Sheet

A really comprehensive infographic, available to download as a PDF, that covers HTML5 tags (old, new, and obsolete), attributes, desktop and mobile browser support, event handlers, and Canvas features.

The HTML5 Mega Cheat Sheet

4. SVG Authoring Guide

An official document from the W3C that ‘describes some best practices for creating interoperable, reusable, performant, efficient, and practical SVG documents, using explanations and code samples.’ The document provides guidance without setting requirements and is useful for developers creating SVGs manually.

SVG Authoring Guide

5. HTML 5.2 W3C Working Draft

Although the WHATWG HTML5 spec continues to be a “living standard” without versions, the W3C is pushing ahead with increments for their spec. If you’re interested in looking at what’s new and different in HTML5.2, this is a good place to start.

HTML 5.2 W3C Working Draft

6. Web Developer Checklist

A nice, extensive checklist for developers when finishing a project. Includes categories for usability, accessibility, SEO, code quality, security, and lots more. Also, each item in the categories includes links to help you check off the items.

Web Developer Checklist

7. Can I animate…?

A simple site that allows you to search for a CSS property and it will tell you if that property is “animatable” and will provide further info on how browsers calculate the animation.

Can I animate...?

Continue reading %20+ Docs and Guides for Front-end Developers (No. 10)%


by Louis Lazaris via SitePoint

Get Started Building a Material Design App

The Best Way to Create Fantastic ‘Invisible Pen’ Effects in SVG

Magic Pen

From the blink tag of the 1990’s to today’s modern technologies such as CSS transitions and animations, the ability to move elements on web pages/applications has been always a tempting goal.

Today, technologies like SVG make it relatively easy to create an effect I’m going to call ‘A Magic Pen’ animation – the viewer watches an illustration appear line by line as if drawn by an invisible pen.

Back in 2013, Jake Archibald very cleverly demonstrated how you could manipulate stroke-dashoffset and stroke-dasharray and some CSS to create this magic pen effect on any SVG linework.

This approach is easy to implement on relatively simple linework, but it’s not always the ideal solution when we want to create something more complex. For more complicated linework animations there are purpose-built tools to help you such as Vivus.js. Vivus is a lightweight JavaScript library which allows us to animate the SVG elements without messing around too much with CSS.

I suppose, now the main question popping up in your heads is: What makes Vivus better solution compared to the CSS approach?

In my opinion, Vivus has three main advantages which should be mentioned:

  1. Ease of use : In Vivus, we set all the animation properties in a Vivus 'constructor object' and in context along with our SVG document. This makes changes more convenient and intuitive. With only a couple of tweaks we can get a completely different animation result.

  2. Automation : As you may already know, the stroke-dashoffset property is available only on SVG path elements. This can be challenging because in an SVG document there are other types of elements such as the circle, rectangle, line, and polyline objects which can easily break the entire animation.

    Fortunately, Vivus is smart enough to automatically convert all SVG objects into path elements making possible to animate them too.

    However, do note that we need to transform all text element manually into paths, using an editor such as Illustrator, InkScape, and alike.

  3. Flexibility: Vivus offers five types of animation with a lot of options for manipulation, which gives us great flexibility about our animation’s behavior.

Now, let’s start exploring Vivus in more detail and see its advantages in action.

Attention designers: Yes, there is some code ahead, but Vivus is meant for designers to use. Even if you’re not a hardcore codemonkey, a little cut, paste and edit will get you some pretty cool results.

Exploring Vivus

To start using Vivus we need to include it in our HTML like this:

[code language="html"]<script src="http://ift.tt/2f0ZWih"></script>
[/code]

Then, we can use an inline SVG in the following manner:

[code language="html"]
<svg id="canvas">
<path...>
<path...>
<path...>
</svg>
[/code]

And the script:

[code language="html"]
<script>
new Vivus('canvas', {duration: 300}, callback);
</script>
[/code]

Here, we have an inline SVG and a Vivus constructor function which takes three parameters:

  • ID of the DOM element we want to interact with (our ID is ‘canvas’).

  • An option object where we put all the animation options.

  • And an optional callback function called at the end of the animation.

Before we move on, we need to know the following:

  • By default Vivus draws elements in the same order as they are defined in the SVG document.

  • All SVG elements must have a stroke property and cannot be filled.

  • We must not put any hidden path elements in our SVG. Otherwise the animation might not be displayed properly.

  • Text elements cannot be converted into path elements. If you want to use text in our animation we need first to transform it into paths (in editor like Illustrator, InkScape).

The Five Drawing Options of Vivus

In the next sections we’ll explore all five animation types offered by Vivus. We’ll use a very simple drawing (four lines) in order to see clearer the difference between the available animation options.

Delayed

See the Pen Simple line animation by SitePoint (@SitePoint) on CodePen.

CodePen

Here our SVG drawing consists of four lines with equal length. In delayed mode, which is the default animation, all paths start almost simultaneously with a small delay between each one.

One-By-One

See the Pen Vivus demo: One-by-one by SitePoint (@SitePoint) on CodePen.

CodePen

In oneByOne mode each path is drawn one after the other. In the example above the same four lines are drawn one by one in a continuous manner.

Continue reading %The Best Way to Create Fantastic ‘Invisible Pen’ Effects in SVG%


by Ivaylo Gerchev via SitePoint

Divided Vision Media

Creative digital agency


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

Personal Portfolio

UI / UX Designer based in Barcelona. I started originally as a graphic designer, but I changed the paper into screens in 2013. This is my site.


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery