Monday, 1 December 2014

How to dynamically generate JavaScript based on templates in the Java/Scala Play! Framework version 2.3.6



1. In /app/assets/views, Create a file “scriptjs.scala.html”
2. In/app/conf/routes, add a route as follows

GET    /script.js     controllers.Application.scriptjs()

3. In app/controllers/Application.java,  add the following method:

public static Result scriptjs() {
String params = “Hello World”;
return ok(views.html.scriptjs.render(params)).as("text/javascript utf-8");
}

4. In your newly created scriptjs.scala.html, add the following

@(params: String)

alert(@params);

5. Now, in your HTML file, add the following tag

<script src=http://YOUR_WEBSITE_HERE.com/script.js></script>

6. If your webpage now pops up an alert message saying "Hello World", you're all done!

Saturday, 8 November 2014

CSS Vertical Align Trick - SASS Version

Came across this link earlier today, and it was extremely clever.

Here is the SASS version of the CSS displayed there.


.wrapper {
  
  &:before{
    content: '';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
    margin-right: -0.25em;
  }
  .row {
    display: inline-block;
    vertical-align: middle;
  }
}

Tuesday, 21 October 2014

How to remove the field_with_errors div

In Rails 3, The field_with_errors div was messing up my column spacing.

I came up with the below solution:

# in application.html.erb

config.action_view.field_error_proc = Proc.new { |html_tag, instance| 
    html_tag.gsub('class="', 'class="with_error ').html_safe;
}

Now you can add the '.with_error' class to your CSS file and edit styling for individual input types.

/*In your application.css(or any other css file)*/
input.with_error {
    color: red;
}
label.with_error {
    color: red;
}

Thursday, 16 October 2014

JavaScript Loop Wrapping (0,1,2...6 and repeat from 0)

Sometimes you want your for loop to iterate over a given set of numbers and, when it reaches the end of the series, wrap around to the first value. For example, something as simple as ,,
0,1,2,3,4,5,6....0,1,2,3,4,5,6....etc


  To do this, you can implement the following for loop

  for(var counter=0, iterations=0; 
    iterations < 20;               
    counter=( counter == 6? 0 : counter+1 ), iterations++ ){ 
        console.log(counter);
}

  //outputs 0,1,2,3,4,5,6,0,1,2,3,4,5,6.....


  Or,

  for(var counter=0, modulus = 0;       
    iterations < 20;                 
    counter++, modulus=counter%6 ){   
        console.log(modulus);
}

  //outputs 0,1,2,3,4,5,6,0,1,2,3,4,5,6.....

Windows 8.1 100% Disk Usage / Overheating Solution - HP Pavilion g6 Laptop

(I use an HP Pavilion g6 Windows 8 Laptop) 

My Windows laptop was always slow, even out of the box. I recently discovered that it was using 100% of its hard drive capacity... i.e., it was reading and writing at 100% speed, all the time.... DRASTICALLY slowing down my machine's performance and causing heating issues as well.

To solve, I did the following

On Windows 8/8.1, First press ctrl+alt+del and go to Task Manager. In the "Performance" tab, see if any of your disc drives are at 100% usage all the time. 

If yes, go to WindowsKey+R and type 'services.msc'. Look for the services "Superfetch" and "Windows Search". 

For both these services, right click and go to "Properties". in the "General" tab, change the 'Startup Type' to 'Disabled' Note: this is NOT the same as stopping the service using right-click!!

That's it! After that, my windows machine has been a veritable speed demon.

Untrusted : A JavaScript Roguelike Programming Game

Untrusted is what you would get if  Roguelike gaming had a lovechild with JavaScript programming, but was abandoned in the woods and raised by Puzzlers.

You play the role of Dr. Eval, who must get to the next level by solving various problems using JavaScript.

Yes, that's editable JavaScript code on the right-hand side. You can edit a lot of different aspects of the game world, including loops, timers, and even AI behaviour.

This is a must-play if you're passionate about JavaScript! Click here to play now.