Creating Asynchronous Functions in Javascript

For Common Curriculum, there are a number of instances where I want a function to execute asynchronously so it doesn’t block the rest of the app. While I’ve worked with async functions in JS, I’ve never created one. Yesterday, I did some googling and found out how. 

Let’s say you have a function like this:

function(){ console.log('hi')}

To have it run asynchronously, you have to wrap it in the setTimeout function with a delay of 0. setTimeout takes two arguments: a function and a delay in milliseconds. e.g.

setTimeout(function(){console.log('hi')}, 0);

To try this out, copy and paste the following into the console and call test. You’ll see it will wait for a second then put “hi” in the console.

function sleep(milliSeconds){
  var startTime = new Date().getTime(); // get the current time
  while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

function test{
  setTimeout(function(){console.log('hi')}, 0);
  sleep(1000)
}

References:

- The sleep function I used 

- Stuff on async JS

Baltimore City Schools, Alonso, and Otis Rolley

This morning, I tweeted out a few thoughts on mayoral candidates and their thoughts on Baltimore City Public Schools (City Schools). I wanted to expound on those thoughts. I’m going to post my tweet then write more about it.

Reading @baltimoresun art on mayoral debate. Otis says it’s heartbreaking the lack of outrage over cheating. What’s really heartbreak [cont]

is the lack of transparency pre-Alonso and the fact NO ONE CARED. Don’t remember Otis campaigning for more transparency in city schools….

In the media, mayoral candidate Otis Rolley has been an outspoken critic of the cheating in City Schools. I heard of cheating at Abbotson (one of the schools that got caught) way back in 2006. If you were plugged in, you knew it was happening. Getting our knickers in a bunch because we finally caught some schools cheating is, to me, misplaced outrage. The greater source of outrage is the culture of low expectations and permissiveness that allowed cheating to go on for the last two, three, four, or more decades. Alonso spent $400,000 to have test monitors at all the schools to prevent cheating. He got serious. I think we should all have a sigh of relief that the district finally got serious about a problem everyone knew existed.

Notably, I thought most of the other elected officials reacted appropriately to the news of cheating. Both Mayor Stephanie Rawlings-Blake and State Ed Superintendent Nancy Grasmick were upset and disappointed that cheating happened. However, they praised the steps Alonso took to ferret out the behavior.

On the other hand, Otis staged a press conference. The Sun reported him as saying this:

However, on Thursday morning, Otis Rolley, one of the mayor’s challengers in the Democratic primary, held a news conference asserting that the mayor and the school system should be held accountable.  Link 

What, exactly, would the mayor and the school system by held accountable to in this case? Not cheating? What would that look like? I think it would look like exactly what Alonso did: hold schools accountable. Otis’ comments don’t make much sense to me.

OTIS’ IDEAS ON EDUCATION

Regarding Otis’s ideas on education, let’s talk about them:

  • Increase accountability for schools’ results by returning control of the schools to the City;

Here’s a solution without a problem. The current state-city run school board has actually worked quite well. It brought in Alonso and has been supportive, though occasionally critical of his work. The only reason I can think for Mayoral control is to control Alonso, which is to say, have the ability to fire him. This greatly concerns me. If Otis could articulate some clear reason why the current system prevents Alonso from doing his work, I’d love to hear it.

  • Help children escape the worst performing schools through limited and targeted opportunity scholarships;

Read: school vouchers. Philosophically, I have no problem with vouchers. Pragmatically, I have no love for them. They were tried these in DC a couple years ago. They didn’t raise significantly raise student achievement (they didn’t lower it either). There are other studies out there which suggest the same: vouchers aren’t bad but they aren’t that great either. To me, we have to ask if the expenditure of time and energy worth it? Is spending .5 - 1 million a year (buses + admins at central office to manage it) worth seeing little to no gains? I’m going to say no.

  • Build or rehabilitate more than 50 schools in 10 years through public-private partnerships; and

Who wouldn’t love more great schools? I’m little shaky on what “public-private” partnerships means. I’d love to hear more.

  • Expand the number and quality of charter schools.

Fantastic idea.

MORE ABOUT MY TWEETS

Let’s talk more about some of my tweets:

Also, mayoral candidate who suggest Alonso could possibly transform city schools faster are *dangerously* out of touch with reality.

Compare Balt to urban districts nationally=Alonso is stellar.Locally, he’s the best thing we’ve ever hard. Perfect? No.Amazing for Balt? Yes

kbladow Kate Bladow @ @scottmessinger You mean schools can’t change overnight? ;)

@kbladow Im going out a limb here and saying no. I know it puts me and Otis at odds, but I’m going to stand on the side of reality and logic 

In the last tweet, clearly, I’m being snide and it’s not fair to characterize Otis as illogical. However, I do believe Otis’s tone and wording suggests he thinks change should happen much faster than it currently is. Going back to the previous quote:

However, on Thursday morning, Otis Rolley, one of the mayor’s challengers in the Democratic primary, held a news conference asserting that the mayor and the school system should be held accountable. "We are not giving a quality education to our kids," said Rolley. "It’s not good enough for my kids, and it’s not good enough for any children in Baltimore City." Link

Truth. Indeed, the City Schools aren’t good enough. No one doubts this. But what are we holding them accountable to be? It’s illogical to expect City School’s to become Howard County next year. That’s not holding them accountable. That’s being ridiculous.

We need to hold City School’s accountable to growing over time. We need to compare City Schools to other large, urban districts. We need to look at trends, not individual data points. Though City Schools’ scores fell last year, they’re still up 12.3 and 13.6 percentage points over 2007 numbers. That’s significant.

I think Otis’ focus on the number of students passing is, by itself, unhelpful. I don’t hear perspective from Otis when he comments on the state of the city schools and that concerns me. 

ON TEACHER RETENTION

 

natemook Nate Mook @  .@scottmessinger My question is what can be done to keep good teachers around? Alonso clearly isn’t doing it, so more CAN be done. You left.
@natemook I didn’t leave b/c schools but for something else. I think Alonso is doing it. With holds principal’s accountable & fires them….
@natemook i think principals are a HUGE reason why people stay or go. Alonso is helping flush out the old, ineffective leadership.I love it.
@natemook I think it would be helpful to know how does Balt’s teacher turnover rate compare to other big, urban districts? Apples to apples.

Again, I believe strongly in apples to apples comparison. Also, I should note that with the new teacher compensation system, applications have gone up two fold. That’s significant. I think Alonso is putting things in place to significantly change teacher turnover and I look forward to seeing the numbers in 3 years.

IN CONCLUSION

I really like Otis. I’m greatly concerned about his tone towards city schools and I worry that he has a narrow perspective on City School’s performance. I’m concerned that if he’s elected and takes control of the school system, he’d fire Alonso over one year of bad numbers instead of looking at multiple years of significant growth. I’d like to see Otis changes his tone and articulate a broader perspective on City Schools. If he does, he’s far more likely to get my support.

Fixing a problem: Rails.vim forces JS files in Rails projects to have 4 space tabs

If you’re like me and prefer to use 4 spaces to indent your javascript files in your rails app AND you use vim AND you use rails.vim, you might have run into the problem. Typical methods like adding the following line to your .vimrc file don’t work with rails.vim:

autocmd filetype javascript setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=4

It appears rails.vim clobbers these settings. To fix this, open up rails.vim in the autoload directory in the vim-rails plugin folder and edit line 4578 to remove the javascript param.

https://github.com/tpope/vim-rails/blob/master/autoload/rails.vim#L4578Change

  if ft =~ '^\%(e\=ruby\|[yh]aml\|javascript\|css\|s[ac]ss\|lesscss\)$'

to   

if ft =~ '^\%(e\=ruby\|[yh]aml\|css\|s[ac]ss\|lesscss\)$'

and everything should work!

If you know of a way to do this without editing the rails.vim file, let me know in the comments!

Knockout.js kills Backbone.js in code needed to create awesomeness

For the next version of commoncurriculum.com, I’ve been experimenting with different client side JS frameworks. I’m loving Knockout.js. It has some weaknesses, but, overall, I think it’s the best combination of power, library size, and simplicity. 

To give you an example, let’s compare the length of code needed to create the backbone.js example. They both use 43 lines of HTML. In backbone.js, it takes 256 lines of JS code. In knockout.js, it takes 40 lines of JS.

In other words, writing the Todos app in knockout requires 85% LESS javascript!

Amazing.

Check out the comparison here:

https://gist.github.com/1033617

Adding Custom Commands to Mac OS X terminal

Problem: I wanted to open up files in Chrome from the terminal.

Solution: I googled and stumbled across this post:

http://robertnyman.com/2007/05/10/add-custom-commands-to-the-mac-os-x-terminal/

To summarize the steps:

1. I opened up a new terminal tab which put me in my home directory

2. I opened up my .bashrc file by typing mate .bashrc

3. I added the following code:

chrome(){
open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome "$1"
}

Now I can type chrome index.html to open the index.html file in Chrome. 

BONUS:

To enable case insensitive auto-completion in terminal, open up a tab (which puts you in your home directory) and type:

echo "set completion-ignore-case On" >> ~/.inputrc

This adds a line to your .inputrc file which turns off case sensitive file completion. 

Submit google forms with AJAX or how to make cross domain posts with ajax

Goal:

I want to collect user sign ups with a google form. I want to submit the form with AJAX, fade out the form, and display a new message.

Problem:

You can’t make cross domain post requests on IE7. As a result, the following won’t work.

$('form').submit(function(){

$.post('http://googlespreadsheets', $(this).serialize())

})

Solution:

1) Add the page you’re posting to in an iframe. Give it a name 

2) Set the target of the form to the name of the iframe.

3) Add JS to hide the form upon submit.

HTML:

Javascript:

Sources:

I found this post helpful:
http://stackoverflow.com/questions/467008/asynchronous-cross-domain-post-request-via-javascript