about this style

I was lazy this time and used someone else’s template. Well, a chunk of it, since I’m incapable of using a template and not tearing it apart to change things. In this case, to make it readable (beyond the expansive white space), and to make it usable. Like, comments. Or not making blockquotes at 50% opacity.

What really bugs me is that it loads so slowly. It’s not even a one-page app, but it’s acting like jQuery on crutches with all the fading in/out. Sure, it’s pretty but damn it feels like my server’s dishing up at a snail’s pace, and I happen to know my server’s pretty speedy.

I’ve been meaning to play in php again, and I guess this gives me an excuse. Not quite like revisiting high school (considerably less drama) but just as nostalgic, in its own crazy way.

demographics, take 4

Now it’s time for word problems. From the original source, Ross says:

To determine the approximate number of ruined fortifications, divide the kingdom’s population by five million. Multiply the result by the square root of the kingdom’s age. If the kingdom has changed hands a lot, use the total age-the number of years that castle-building people have lived there, regardless of the Royal Lineage. … Assume one functioning castle for every 50,000 people [and] 75% of all castles will be in the civilized (settled) areas of a kingdom. The other 25% will be in the “wilderness,” along borders, etc.

This is the kind of thing that keeps me up at night. more

ng-repeat times two

I had a plunker all worked up, trying to figure out how to loop a random ng-repeat inside another randomized ng-repeat. Unfortunately it’s just like doing a for loop anywhere else: the outer loop won’t increment until the inner loop is done.

Then I realized that given what I’d be using it for, this would be two ng-repeats inside another ng-repeat. Just like in PHP, that gives me an index or iteration number, so all I have to do is pre-sort the two arrays (colors and animals, in the example plunker). Then I’ll just use the index to get the first from array #1, and the first from array #2, like arrayOne[1] and arrayTwo[1]. As long as each remain steady, I won’t get duplicates. (If they resort with every iteration, it’s slower performance and it ups the chance of repeats.)

Whew, that saved a lot of time. Always good to find an easier way to do things.

demographics, take 3

See live version.

Now that I’ve got a lot of the variable-picking in place (land area, climate, % arable, type of diet, etc), time to tie all these things together. The easiest way is with a watch, but angularjs now lets you do a whole bunch of watches strung together in a watchCollection.

Adding ‘true’ at the end is because… I don’t know why it’s because. One of those Stack Overflow Gods suggestions, and it doesn’t seem to be hurting anything. It’s just supposed to be good form. more

demographics, take 2

See the current version.

Housecleaning continues, and just like actual housework, go for the lowest-hanging fruit first. In this case, it’s the repeating math formulas. I had a bunch, all lined up and basically doing the same thing, just with different variables. more

using angularui/bootstrap

I’ve been using bootstrap for almost a year now, and it’s a well-rounded framework, especially if you’re already using jquery and are gonna frontload that monster anyway. Not sure I see the point, if I’m using angularjs, especially since a major objective is getting out from under the morass that is jquery. (Don’t get me wrong, I happen to like jquery a great deal. It’s just a there’s an awful lot of it, loading-wise.)

Some awesome soul came up with angularjs substitutes for a chunk of the bootstrap js, which you can find at the angularui github. All I wanted was one little functionality, but I loaded up the entire thing just in case. Haven’t decided yet whether to strip it down to just that one thing. I might find use/need for some of the other directives.

Adding it in really is as easy as the instructions. Just inject it at the module level.

Make sure to call the cdn (or use it locally if that’s your thing), then just follow the patterns provided in the examples.

If it’s not really obvious, tabset and tab are calling the directives. The one part of the obstacle course that’s harder when you’re learning is that every example has two or three variations. Fortunately there are links to plunkers, and once I’d sandboxed there to get it working how I wanted, with the version I wanted, I ported it over into my own code.

It’s so easy, it’s almost tempting to try and come up with some reason to use one of the other directives. Trying to refrain is the only hard part about angularui.

demalforming json

After repeating the basic pattern of user input + ng-model + $scope.whatever = function(), things are starting to get really messy. Need to do some house-cleaning, and the various json-like things in the controller are the first to find a new home.

I kind of like the whole research thing, and I like when I can get a real-world idea of things, so naturally I figured I’d throw a few in there, for anyone else who does the same. For the first draft, I put all that in the Ctrl controller, until I knew for certain that I had all the info I wanted and it was otherwise working. Then it’s time to detach. more

demographics, take 1

For whatever reason, I was pondering the demographics of early modern cities. I found S. John Ross’ Medieval Demographics Made Easy, and it seemed like a great way to play in angularjs without work’s attendants headaches of breeze, require, node, and the rest of those tangles. Just basic angularjs… with a lot of math. I don’t get to do math much, at work.

See the current version.

As usual, I started with a simple plunker. I tend to use plunkers as sandboxes, at least until there’s enough code in any one file that I’m scrolling. Then I move it offline. Anyway, that plunker’s the first iteration. Everything’s in the controller, at this point. It’s another kind of sandbox, and I dump everything in there while figuring out the logic. I’ll start moving things into factories, services, directives, later. For now, just trying to get things to work. more

calling a controller

There seems to be a lot of ways to call your controllers.

I can’t recall ever seeing that using function + controller name is bad, so much as not-good. I see it around, but for awhile I wasn’t sure how to translate that into the better format, so here they are side-by-side.

Except if you minify your controller, anything you inject ($scope and $window in this case) will get totally munged. Best to provide names for whatever you’re injecting, like so:

(Notice the brackets keep getting added onto, at the end.)

But then if your controller’s going to end up in a callstack, it’s not quite munged but it will be anonymous. So best to name your controller like this:

And then after all that I found out I didn’t really need to say $window.Math for every calculation, that I can just do Math, which means I don’t even need the $window injection, either. Figures.