Computer Programming

Yes, a bit specific, but I need to store some links!

DOM tests in Jasmine

This continues some notes about working on sf-active-js, moving it to a contemporary Javascript development environment.

Here's a Jasmine spec to instantiate an HTML5 widget and test a few of the method calls. These method calls cause changes in the DOM, and these tests verify that the changes happened.

describe("comment", function() {
    var comment;
    beforeEach(function() {
        var elements = $('<div id="disclose"></div><div id="edit"></div>');
        $('body').append(elements);
        comment = Comment('#edit', '#disclose');
    });

Browserify libraries that can be tested with Jasmine... before they are rolled up by Browserify

This is part of a series of notes about bringing a JS project, sf-active-js, over to a contemporary Javascript development environment.

This is a trick to write modules that are testable in a browser, without subjecting the test to Browserify. Why?

I want to develop without the compilation step. It's easier and faster to work in the browser. (This particular app is mostly DOM manipulation, so headless isn't important.) Once the module has settled down, it can be added to the library.

The rest of this note assumes you know the Jasmine sample test runner included with the distro.

Notes on migrating to a contemporary javascript development environment

I'm writing this after writing the first two notes in this series about migrating sf-active-js to a contemporary Javascript development environment.

I wrote the original experiment a couple years ago. It's a JS client that retrieves some json data from the la.indymedia.org server, and displays it. I ran out of time, and didn't finish it then.

Upon revisiting the code, I found it a little confusing. It was around 800 lines of jQuery-style JS.

Node Package Manager, Bower, Yo, Jasmine Beginner Notes

Some rough notes related to the JS development toolchain. I've known JS for years, and have done Crockford-style dev a while, but this JS toolchain stuff is mindblowing and pretty complex. The docs are also pretty confusing. So these are rough notes. Not really a tutorial or the voice of experience.

Local, Global

"Local" means "local to the project".

"Global" can mean "global to the computer" or "global to the user, meaning really local to the user, but global across multiple projects."

It's a bad idea to install things globally to the computer system.

d3 dispatch (the d3 event system) notes

Learning d3 events this afternoon. These are the notes I'm taking while I'm learning it. I hope it helps.

It's called d3.dispatch. d3.dispatch('eventname1','eventname2') returns an object that manages setting event handlers, and dispatching events. The event system isn't global (unlike in most frameworks, where the event system appears to be globally available)- it's contained entirely within the object.

var dispatch = d3.dispatch('say');

That created an object that has a method dispatch.say() that dispatches/rasies/throws/broadcasts the "say" event to the listeners.

Formidable: some kind of wonderful

Formidable is a magical library to make HTML forms much better.

With it you can turn a form into an object that will validate input data, generate arrays of error messages, and tell the user to fix problems. It's pretty amazing because, unlike other form builder libraries that build up a form via PHP, you write your form in HTML, and then it's magically parsed and transformed into the form object. So you can design the forms in BlueGriffon or any other visual HTML editor.

How many steps are there in a simple user registration and login?

We all take it for granted that most sites will have a user registration system, and way to log in. We're used to it, and most people are unaware of how complex it is.

So, how complex is it?

It's around 15 different steps or screens.

Fifteen.

I've done it a dozen times, and I think that's about right.

Bitten by the "this" scope issue in JavaScript callbacks

I took a step away from Angular and jQuery style coding conventions and dropped back to the traditional JS-style OOP, mainly because I wanted to get familiar with regular JS programming using the MDN docs, and learn the new HTML5 calls (which aren't new anymore).

I ended up coding using an MVC style, and consequently, added an event handling system. It was easy-but-cheesy so I eneded up using Backbone's event system. (It makes me look more hip.)

BTW, here's one way to use Backbone events within traditional JS code:

FooController = function() {
  var obj;

Parallax Effect

This is a short snippet of code to do the popular "parallax" effect for headings. This code has two boxes. The second box helps you see how to deal with a box that's not at the top of the page.

There's not much to it. You make some boxes and set the background-position based on the window.scrollY value.


<html>
    <head>
        <style type="text/css">
            #box1 {
                height: 350px;
                background-image: url(trafficjam.jpg);
            }
            #box2 {
                height: 350px;

Shorter code with functional-style programming in PHP.

So, I'm doing wordpress programming, and one of the headaches is that they store serialized objects in the database with the update_option() and related functions.

This makes querying some data difficult. Here's an example of the data structure I was dealing with (it stores sidebar settings).

Array
(
    [wp_inactive_widgets] => Array
        (
        )

    [sidebar-1] => Array
        (
            [0] => search-2
            [1] => recent-posts-2
            [2] => recent-comments-2

WordPress Unit Tests in the Main Directory

I'm hacking some of the original WP files. Not the smartest thing to do, but the options are to replace the functions, or alter them, because a plugin isn't going to work.

So, I needed to run tests in the main program. Here's the quck howto.

Install wp-cli and get it working.

Then, go into your wp root and type:

wp scaffold plugin-tests --dir=.

Then run the installer script:

bin/install-wp-tests.sh wordpress_test root '--password--'

WordPress wp-signup headaches, and refactoring.

I've been modifying the wp-signup script a lot, and figured out a better pattern for doing these screens. This technique works more like "views and controllers", so it's one step closer to MVC.

WP doesn't have routes, but the wp-signup script has a paramter named "stage" that specifies the next state. It's effectively a route.

There are two types of stages: showing a form, or validating a form.

When you validate a form, you see if there are errors. If there are, you show the form, with error messages. If there are no errors, you commit the changes, and then show the next form.

What is Dependency Injection?

I've answered this a couple times so I'm writing it down :)

DI is a way to separate services from the code that uses those services.

DI makes testing easier. It also allows the services to be replaced with alternatives - but this is less important than the testing.

DI is used in Angular, Zend Framework 2, Symfony and more frameworks.

DI is implemented as an array or dictionary of services with generic names. Services are created during startup, and registered.

Django 1.8 Tutorial - 5.2 Adding a User Profile

In the previous section, I noted that I needed to learn how to make my own authenticator. I also needed to learn to add fields to the User. There are two ways to do it, and the Django docs don't clearly state which is better, for what situations.

I think the old way of adding fields, called User Profiles, is the way to go. It's more code, but I think the code ends up being a little more explicit and easier to read. It also allows you to make multiple different profiles for different things.

Django 1.8 Tutorial - 5.1 Alternative Authentication (Make Your Own)

I was hoping to get into Allauth, or maybe that and some more UX tweaks, but ended up hitting some walls. We're trying to implement an authenticator that works with an external service, but it's more complex than expected. (I won't be posting it here.)

To get to the point where I could even write parts of the authenticator, I needed to teach myself how to write authenticators. So, here are two authenticators. Both are a little odd, but I think the code is short enough that it won't be confusing.

Syndicate content