Monday, July 27, 2015

Web Design Weekly #196

Backbone.js Basics: Bringing an App to Life with Events

In a previous tutorial, we plunged into the workings of Backbone.js, an MV* JavaScript framework for building applications. Building a small surf shop app (to help us keep track of surf boards in stock), we looked at creating models, grouping model instances into collections, and iterating over collections to create views. We also took it a step further by rendering each of our model instances into its own view, and grouping them together in a parent view. This is where we left off.

In this tutorial, we’re going to see the importance of that last step, and introduce some controller-type logic into the picture. I touched on controller-type logic and where it belongs in the last article, but let’s just drive the point home before we continue.

Separation of Concerns

In an MV* framework, views are typically responsible for the following:

  1. Rendering the UI, and displaying the data retrieved from the database and/or collections to the end user.
  2. Handling user input via events, and somehow communicating those events to the model.

The “somehow” part is where the controller logic comes in. In Backbone.js, we’re able to bind events to a view during its instantiation. If we wanted to add or remove stock, or delete a model completely, we’re able to do so. We’ll go through how to do this step by step, but for now, just imagine that we have a button in our view that allowed us to remove one item from stock. We can register that event in the view, which is essentially controller logic. When that button is clicked, we communicate with the respective model, and make the necessary updates to it. Changes in models can also trigger events that the view can handle, ultimately re-rendering the view and displaying the correct data.

So in a nutshell:

  1. A view renders model data, and registers events.
  2. When an event is fired via user input, we can run some kind of callback or function which communicates with the model.
  3. Inside the model, the logic gets performed. In a real world scenario, you’ll probably also be updating a database here.
  4. The model change fires off an event.
  5. The view picks up on that event, and acts accordingly, possibly re-rendering itself.

With all that in mind, we now need to think about how we’re going to register these events in the first place. Let’s move on to that.

Using Backbone Events

According to the Backbone events documentation:

Events is a module that can be mixed in to any object, giving the object the ability to bind and trigger custom named events.

There are many ways to handle events in a Backbone application, but we’ll be leveraging two ways:

  1. We’ll use the catalog of built in events in tandem with the listenTo method, which tells an object to listen to a particular event on another object.
  2. We’ll also delegate events using the events hash directly inside the view.

Let’s first take a look at leveraging the events hash inside the view instances.

Leveraging the Events Hash

Here’s a recap of the current code of the SurfboardView from last time:

[code language="js"]
var SurfboardView = Backbone.View.extend({

tagName: 'tr',

template: _.template($('#surfboard-template').html()),

render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
}

});
[/code]

You’ll notice that each view instance gets wrapped in its own tr element, which is populated from the template we made before. Let’s edit that template a little bit to include a couple of buttons that will allow us to add and remove stock. We’ll also need to update the markup for the table:

[code language="html"]

Manufacturer Model Stock Add/Remove


[/code]

Each of our view instances should now have two buttons, but they do nothing at the moment. Let’s take a look at the events hash now, and see how we can register clicks on those two buttons. An events hash in Backbone generally looks like this:

[code language="js"]
events: {
'event target': 'callback'
}
[/code]

Continue reading %Backbone.js Basics: Bringing an App to Life with Events%


by Nick Salloum via SitePoint
< %= manufacturer %> < %= model %> < %= stock %>

How to List Categories in WordPress Using the Categories API

In a previous article we saw how to easily work with categories in WordPress. We covered how we can sort our posts into different categories, which can then be edited or deleted.

Having covered the basics, it's now time to have a look at something that will be of more interest to developers: the Categories API, and how to retrieve and display category data.

Like the Links Manager API, the Categories API is a big topic, even bigger in terms of the number of functions available. In this article, we'll be covering one function that is useful when we want to list our categories.

Listing Categories

In the Links Manager API we find an important function that allows us to list our links. It's no surprise therefore that we find the same thing in the Categories API: wp_list_categories(), this is the function we'll work with here.

Basically, you have to call this function in the place you want to see your categories listed.

Continue reading %How to List Categories in WordPress Using the Categories API%


by Jérémy Heleine via SitePoint

Cloud Connected NeoPixels Using The Particle Core

NeoPixels are a bright and colorful way to draw attention to wearable clothing, Arduino powered robots and more. As wonderful as all of those possibilities sound, there has been one thing I've wanted to try for a while. I've always wanted to control my NeoPixels via the cloud using a Particle Core (previously called a "Spark Core").

Particle Cores are microcontrollers that are quite similar to an Arduino Nano, however they have inbuilt Wi-Fi and come with a cloud based service that makes it easy to control your Core from afar. You can find out more on the Particle website. There are two newer versions of the device currently available for pre-order, the Particle Photon and the Particle Electron. The Photon has better reliability and is overall a faster and better upgrade to the Core. The Electron goes a step further and provides 2G/3G connectivity in over 100 countries, doing so via a global subscription service. It brings connectivity to a whole new and exciting level!

If you're looking to get started and you don't currently have a Particle device in your possession, try pre-ordering one of the newer options. At the time of writing, the Particle Cores are now all sold out! The demo below should still work on the newer devices, assuming that the NeoPixel library doesn't have any compatibility issues with the Photon. My Photon is still in the mail, so I can't check that just yet!

NeoPixels are a really neat brand of LED panel from Adafruit that can be hooked together in many different (and super colorful) ways. The NeoPixel grid I'll be using is an 8x8 NeoMatrix grid.

What We're Building

In this demo, we will be displaying a smiley face that will change depending on the mood sent to our Particle Core. If we send it a happy emotion, it will smile. If we send it a sad one, it will frown. We will send these emotions to it directly via very simple POST requests (you can replicate these by creating a form, AJAX site or server to initiate them, we will be keeping it simple, short and sweet by not defining one particular type of POST request method in this article).

The code and explanations for this demo assume you are familiar with your Particle Core, have set it up on your local Wi-Fi network and are familiar with how to flash code onto it via the Particle Build IDE. If you are new to the Particle Core and haven't tried anything with it yet, head to the Particle docs and read through the explanation on flashing apps first. Try flashing a simple LED blinking app to make sure you are all set up and ready to go. Assuming your Wi-Fi connectivity is pretty good, this should be a pretty smooth process.

The Demo

Want to skip ahead and see the final code? It is all available right here on GitHub.

The Sketch

Our sketch for our cloud connected, Particle-powered NeoPixel looks like so:

Particle and NeoPixels Sketch

A few important points when putting together the sketch above:

  • We have a 4700μF, 10V capacitor (Adafruit recommends at least 1000 μF, 6.3V or higher). The one I used was from AdaFruit. They very strongly recommend placing one of these in your set up before connecting the power.
  • There is also a 330 Ohm resistor between the data output pin and the input to the NeoPixel (Adafruit recommends somewhere between a 300 to 500 Ohm resistor). Whilst it might work without this, it is safest to include it!
  • Adafruit also strongly discourage connecting the NeoPixels to a live circuit. At the very least, always connect ground first, 5V and then data. When disconnecting it - disconnect in the reverse order.
  • We are powering the NeoPixels via a battery as the Particle Core does not provide the 5V power on its own that the NeoPixels need to light up. In this case, Adafruit recommends you power the pixels first, before powering the Particle Core.
  • If you don't have a battery pack but understand how to use a logic level shifter to get it up to 5V, you can do that instead. I don't have a logic shifter to try this out!
  • I am definitely not an electronics expert and strongly recommend reading up on NeoPixels via the NeoPixel Uberguide before putting the above sketch together. NeoPixels can be pretty easy to damage so be careful! All I can say with confidence is that the above sketch I made did not damage my own NeoPixels and theoretically should be okay. Make your own judgements first before trying it at home.

The Code

Everything we will be creating will be coded up within the Particle Build Web IDE that we use to flash code onto our Particle devices over Wi-Fi. It uses a similar simplifed version of C++ to Arduino along with the same software library called "Wiring" for common input/output functions. In other words, if you are an Arduino fan, you'll be coding in pretty much the exact same way.

We start by creating a new app within the Build IDE and adding a rather important library to it. To add pre-existing libraries to your Particle apps, select the Libraries option which looks a bit like a bookmark in the bottom left corner.

From the screen that appears, choose the "Neopixel" library underneath "Community Libraries", once it loads click the button that says "Include in App" to add it to the app we are creating.

That will add in the following lines to your app:

[code language="c"]
// This #include statement was automatically added by the Spark IDE.
#include "neopixel/neopixel.h"
[/code]

Notice it says something about "Spark"? I mentioned it briefly at the start of the article but thought I'd point this out once more to avoid confusion. The name "Spark" will appear throughout the code as it was the old name for "Particle". I'd say the name will fully change over soon but my demo still has the old name in it. I'm not quite sure if they've updated the inner workings of their API or not yet.

After that, we define three constants for our app. We define the pin the NeoPixel grid is connected to, the number of pixels in our grid and the type of NeoPixel we have.

[code language="c"]
#define PIXEL_PIN D7
#define PIXEL_COUNT 64
#define PIXEL_TYPE WS2812B
[/code]

We then define our Adafruit_NeoPixel object with those three constants.

[code language="c"]
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
[/code]

Continue reading %Cloud Connected NeoPixels Using The Particle Core%


by Patrick Catanzariti via SitePoint

Appixel

Appixel is a clean and modern One Page Showcase HTML template appropriate for Mobile Apps.


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

West Side Swordy

State-of-the-art motion graphics and studio-quality 3D animations and sound design; advanced, interactive Javascript/CSS parallax animations and epic soundtrack; clever memory-game with iconic movie sound bytes.


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

Those Guys Aps

We create beautiful games and apps for everyone on earth.


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