Inspecting Angular Forms

Thursday, Oct 24, 2013 11:38 am
William Barnes

I have a long, multi-part Angular form on my <a href=”” title=”Toronto Uncontested Divorces – Lawyer Prepared”>new website</a>. At a certain point, I began to find it difficult to review the source to ensure that the model being produced lined up with the output I wanted, so I wrote a bookmarklet that displays the ngModel attribute above each form control.

The current version unfortunately requires jQuery to be used on the page.

  1. javascript:$('.debugAngular').remove();$("[ng-model]").each(function(i, el){ var pos = $(el).offset(); varNewEl = $("body").append('<div style="position:absolute;z-index:9999;top:'+('px;left:'+pos.left+'px;background:#000;color:#fff" class="debugAngular">'+$(el).attr("ng-model")+'</div>') });

Dojox/Socket, Node.js, and

Monday, Sep 16, 2013 10:57 pm
William Barnes

I’ve been playing around with node.js and I decided to try setting up a web socket connection. I’m using on the server side and Dojo Toolkit on the client side. Things on the server-side were straightforward. However, I decided to give dojox/socket a shot and got the following error:

-debug- destroying upgrade

As it turns out, dojox/socket is a fairly plain wrapper over the browser’s built-in Websocket object (with long-polling support added). does rather a bit more and (rightly or wrongly) seems to prefer talking to itself. Luckily, if you’re not too picky, you can easily include the official (AMD-compatible) client:

  1. require([
  2. '../'
  3. ], function(io) {
  4. var socket = io.connect('ws://localhost:8000');
  5. socket.send("Hiya server!");
  6. });

Note that the proper relative path to the file has to be specified (the proper version of this file is automatically served by through node.js).

Capifony fails on “bin/vendors install –reinstall”

Tuesday, Apr 3, 2012 10:28 pm
William Barnes

I was attempting to deploy my Symfony app using Capifony, but it just wouldn’t work. The first error in the train wreck was: “Warning: Wrong parameter count for parse_ini_file() in …/bin/vendors on line 69”. Like all problems that take forever to fix, this one was incredibly simple. My shared host had both php 5.2 and 5.3 installed. I added the following line to deploy.rb: set :php_bin, "/usr/local/bin/php-5.3"

Your path may differ.

Update: If you are having this problem, you also need to fix $interpreter in ‘bin/vendors’.

HTML5 History API and Scrolling

Tuesday, Apr 3, 2012 7:22 pm
William Barnes

[Edit: The solution below has a bug. If the page is refreshed, then the states array is lost. I have an updated approach and will post it soon.]

My new web app uses the HTML5 History API (as abstracted by History.js) to smooth out navigation. This proved to be a bit of a challenge because, despite all the Javascript, my app is supposed to act just like a normal web page. The main content of my app is dynamically loaded in an ordinary static-positioned DIV. Thus, the user scrolls the window like normal. Other elements are fixed to the viewport.

One major problem I encountered was that browsers have serious issues with remembering the window scroll position across pages Normally, when a user clicks the back button, they are taken to the previous page at the same place that they left it. Most browsers don’t actually reload the page, but store it in memory, so this happens almost instantaneously. When using the History API, this does not always work. In this post, I detail the problems and my solution.

In Webkit, when the user navigates (using links or the back/forward buttons) the scroll does not change from page to page unless the page is too short to support the scroll value. If the user scrolls on page 1 and clicks to page 2, window.scrollY on page2 is the same as it was on page 1. If they scroll on page 2 and navigate back to page 1, scrollY is now the new value. If page 3 is not long enough to scroll, then scrollY is zeroed. Going back to page 1, it remains 0. I like this behaviour because it is predictable. If I am overriding the browser’s navigation, I’d like to have complete control.

Firefox, however, is somewhat erratic in this area. Generally speaking, it behaves like a browser on a normal website. If scrollY on page 1 is 50 and the user clicks to page 2, scrollY is 0. If they scroll a bit so scrollY on page 2 is now 200, then navigate back to page 1, scrollY will be 50. However, if they navigate to page 3 (which doesn’t scroll) and then back to page 1, scrollY will be 0.

I worked around this by storing the scroll value myself, and scrolling to that on each History statechange. Originally, I only stored the current scroll value while handling the statechange event, but Firefox applies its remembered value before firing the event. This placed me in the awkward position of needing to know the value before the user did anything. I use the window.onscroll event to detect when the window has been scrolled by the user so that it does not store a value in the awkward time where new content is loading. Following John Resig’s example, I store the value every half second using setInterval.

  1. var loc = {
  2. currentState: 0,
  3. states: [
  4. {scroll: 0}
  5. ],
  6. hasScrolled: true,
  8. initHistory: function() {
  9. var History =;
  10. if (History.enabled) {
  11. // Before binding events, replace the state with the proper ID.
  12. // Otherwise, strange things happen.
  13. History.replaceState({state:0});
  14. // Bind the state change event (Dojo won't do this for some reason)
  15. History.Adapter.bind(, "statechange", loc.handleStateChange);
  16. // Bind the anchor click
  17. on(baseWin.body(), "a[href]:click", loc.anchorClick);
  18. // Bind the scroll event
  19. on(, 'scroll', function() { loc.hasScrolled = true; })
  20. // Store the scroll position every 500ms
  21. setInterval(function() {
  22. if (loc.hasScrolled) {
  23. loc.states[loc.currentState].scroll =;
  24. loc.hasScrolled = false;
  25. }
  26. }, 500)
  27. }
  28. },
  30. anchorClick: function(e) {
  31. var History =,
  32. rootUrl = History.getRootUrl(),
  33. node =;
  35. // Remove orphaned states
  36. loc.states = loc.states.slice(0, loc.currentState + 1);
  37. // Initialize the new state
  38. loc.states[loc.currentState + 1] = {scroll: 0};
  40. // If there is a span, b, i, etc inside the link, that will be the target node.
  41. // This loop finds the actual anchor.
  42. while (node.nodeName.toLowerCase() != 'a') {
  43. node = node.parentNode;
  44. }
  46. var url = domAttr.get(node,"href"),
  47. isInternalLink = url.substring(0,rootUrl.length) === rootUrl || url.indexOf(":") === -1;
  48. if (isInternalLink) {
  49. event.stop(e);
  50. url = url.replace(rootUrl,"")
  51. History.pushState({state:loc.currentState + 1},"",url);
  52. }
  53. },
  55. handleStateChange: function(e) {
  56. var History =
  57. state = History.getState();
  59. // Load the state id
  60. loc.currentState =;
  63. // onSuccess: function() {
  64., loc.states[loc.currentState].scroll);
  65. // }
  66. }
  67. }

Naturally, the above code would have to be modified for another application, but I hope that the method is clear and is of use to someone.

Mobile Safari bugs so far

Sunday, Apr 1, 2012 2:23 pm
William Barnes

I just bought my first iOS device (the new iPad) and I’ve been busy squashing bugs in my latest project. And by bugs in my project I mean, of course, bugs in Safari. Here are some of the more annoying ones and how I fixed them.

Changing CSS visibility breaks things

Several of the problems I had seem to have been caused by various problems with Safari’s handling of visibility. For example, a number of elements in the app use the following CSS transition to fade in and out:

  1. .hideable {
  2. opacity: 0;
  3. visibility: hidden;
  4. transition: visibility 0.5s linear 0.5s, opacity 0.5s ease;
  5. -moz-transition: visibility 0.5s linear 0.5s, opacity 0.5s ease;
  6. -webkit-transition: visibility 0.5s linear 0.5s, opacity 0.5s ease;
  7. -o-transition: visibility 0.5s linear 0.5s, opacity 0.5s ease;
  8. }
  10. .visible {
  11. opacity: 1;
  12. visibility: visible;
  13. transition-delay:0s;
  14. -moz-transition-delay:0s;
  15. -webkit-transition-delay:0s;
  16. -o-transition-delay:0s;
  17. }

I use visibility for two reasons: (1) elements can be sized according to their contents before being shown, (2) visibility can be transitioned. The second reason is important, because otherwise it would require additional Javascript to delay the hiding of the element until after the opacity transition runs. This works in Desktop Safari, Chrome, Firefox, the Android Browser, and Mobile Chrome. In Mobile Safari, however, the element does not get shown until you click on another link.

[Edit: Apparently I was wrong about the Android Browser. I suspect this may be a problem in an older Webkit, since it definitely works in Mobile Chrome.]

I originally solved this by disabling the visibility transition in iOS, but I ran into another problem. Merely toggling visibility breaks Mobile Safari’s new native “overflow: auto/scroll” functionality. I’ll talk about this problem in more detail later. For now, the solution is to disable the transition and use display instead of visibility in Mobile Safari:

  1. body.iOS .hideable {
  2. display: none;
  3. opacity: 1;
  4. -webkit-transition: none;
  5. visibility: visible;
  6. }
  8. body.iOS .visible {
  9. display: block;
  10. }

Overflow scroll breaks on visibility toggle

iOS5 finally introduced scrolling within an element. Previously, this had to be achieved with Javascript (e.g. iScroll). Frankly, I was pretty happy with my Javascript solution, but some variation of the position fixed bug mentioned below was breaking it. The touchable portion of the inner DIV scrolled with the document, rather than remaining fixed.; if the document was scrolled partway down, the fixed element no longer scrolled.

iOS’s solution is fairly simple, just add the following to your CSS: “-webkit-overflow-scrolling: touch;” I imagine it’s done this way because they’re afraid of breaking apps that relied on overflow-scroll just not working. It works pretty well, though it does not use the custom scrollbars. It also does not work if the scrolling DIV is contained in an element that was previously visibility-hidden. This time, it has nothing to do with transitions. The only fix I can find is to use display instead of visibility.

 Javascript scrolling breaks touch events on position-fixed objects

This was a weird one. I put buttons on the right edge of the screen to allow a user to page up and down the document. They were position-fixed (supported in iOS 5!) and they worked. Once. You couldn’t click them a second time. If you clicked one, all the other position-fixed buttons stopped working. If you then scrolled normally, everything started working again. Interestingly, if I just scrolled down 100px, the button would stop working, but if I then touched a spot 100px above the button, it would register. It’s as if Safari has a map of elements for the purpose of registering touch events that stays fixed to the page (ie: position absolute), while the visible representation of the elements stays fixed to the screen. Thus, when the page scrolls, the map goes with it. For whatever reason, Javascript doesn’t trigger a recalculation of where the elements are.

I disabled the buttons in iOS. Apple is aware of this, so a fix may be coming.

Enable PHP autocompletion in Eclipse (Ubuntu 11.04)

Monday, Oct 3, 2011 1:29 pm
William Barnes

For some reason, Eclipse (using PHP Development Tools) doesn’t include built-in PHP functions with a new project. Assuming you downloaded Eclipse from the Ubuntu repository and added PDT through Eclipse’s “Install new software” dialog: in Eclipse, right click on your project in the Explorer pane; choose Include Path, Configure Include Path; select the Libraries tab; click Add External Source Folder. The folder you need may vary depending on the version of Eclipse, it should look something like “~/.eclipse/org.eclipse.platform*/plugins/org.eclipse.php_core*/Resources/language/phpVERSION”.

WordPress Settings Form Helper

Monday, Sep 12, 2011 4:36 pm
William Barnes

I am currently writing my first WordPress plugin. I attempted to use the Settings API on my plugin’s settings page, but found it was a little cumbersome. So I wrote a helper to cut down the number of functions I needed to define. My plan was originally to simply automate the API calls, but it quickly outgrew the Settings API and does quite a bit more. I am posting it, hoping that it will be of use to some other plugin developers. The class handles form creation, display of options, security (wpnonce, back-end validation), and storage.

Read the rest of this entry »