Blog

Custom console.log w/ timestamps

window.log = function(){
	var now = new Date();
	var timestamp = now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();

	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if(this.console){
		console.log(timestamp.toString(), Array.prototype.slice.call(arguments) );
	}else{
		alert( timestamp.toString()+Array.prototype.slice.call(arguments) );
	}
};

// Example
...
mixes : function (sort,tag) {
	log('Router selected route: mixes',sort,tag);
	...

(more…)

Refactoring jQuery call

// v1
var btn = $('#showPlayer');
(state !== -1) ? btn.show() : btn.hide();

// v2 That's more badass:
$('#showPlayer')[(state !== -1) ? "show" : "hide"]();

// v3 even better (only when `state` is a Boolean, not now):
$('#showPlayer').toggle(state);

Extend Backbone View with mixin

In several cases you need to provide the same functions inside your views that do the exact thing. For example the already implemented (but empty) render(). You might also need your special ones like show(), hide(), bindKeys(), setHeader(), etc..
One solution can be the following: create a plain js object with methods you want to share between views,

Device = {

	/* ... */

	Navigation:{
		/* ... */
		methods:{
		
			fire:function (/* ... */) {
				/* ... */
			},

			rebindKeys:function () {
				/* ... */
			},
			bindKeys  :function () {
				/* ... */
			},
			unbindKeys:function () {
				/* ... */
			}

		}
}};

then instead of define your view object with SearchView = Backbone.View.extend(, use the following syntax:

SearchView = Backbone.View.extend(
	_.extend({}, Device.Navigation.methods, {

		el        :'#viewSearch',
		events    :{
		/* ... */

Now after instantiate a view app.searchView = new SearchView();, you can call app.searchView.bindKeys() or whatever you attached to your view.

If you call a shared method while instantiate a view, don’t forget to close that method with a return this; line.

		/* ... */
		show:function () {
			this.unbindKeys();
			this.bindKeys();

			return this;
		},
		/* ... */

Hope you like it :)

Happier way to convert string to json

try{
    return JSON.parse(my_string);
}catch(err){
    return JSON.parse('"'+my_string+'"');
}

Log SPI application pageload with Google AnaliticsOldalváltás loggolása Google Analitics segítségével SPI alkalmazások esetén

var url = "your_new_ur_herel";

// track the page view
if(typeof _gaq !== 'undefined')
	_gaq.push(['_trackPageview', url]);

var url = "your_new_ur_herel";

// Oldalváltás követése
if(typeof _gaq !== 'undefined')
	_gaq.push(['_trackPageview', url]);

SELF is a protected namespace in IE. Avoid using it as a global variable name

	$('.worker').hover(function(){
		self = $(this);

Will throw Not implemented error in IE. Others don’t care. Use me, _this, _self, selfy, pony instead.

DEMO: http://jsbin.com/irikus/5

Use jQuery promise() to create single callback after animate() on multiple itemsTöbb elem animálása utáni callback készítéséhez használj jQuery promise()-t

$('.listContainer .element').animate(
	{opacity:0},
	{
		duration:2000,
		complete: function(){
			// This would be called on each item
		}
	}
).promise().then(function() {
	// This will be called after all items faded
});

OR

var items = $('.listContainer .element');

items.animate(
	{opacity:0},
	{
		duration:2000,
		complete: function(){
			// This would be called on each item
		}
	}
);

items.promise(function() {
	// This will be called after all items faded
});

is identical.

Read more: http://net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises/

Biggest WTF? moment lately

http://en.wikipedia.org/wiki/HipHop_for_PHP

HipHop programmatically transforms PHP source code into C++ and then uses g++ to compile it to machine code.

HipHop for PHP can substantially increase the speed of PHP applications. Improvements of web page generation throughput by factors of 2 to 6 have been observed.

Simplest Facebook like-gate landing page

<!doctype html>
<html>
<head>
    <style type="text/css">
        body{
            background: url(/public/img/bgLike.jpg) no-repeat top left;
            width:810px;
            height:720px;
            overflow:hidden;
            padding:0;
            margin:0;
        }
    </style>
</head>
<body></body>
</html>

surplus commas in JS Objects cause parse error in IE

...
  },
    view : {
      init: function (){},

      showPopup : function(){
        // auto fix
      },
    },
    controller : {
...

Will throw parse error in IEs cause of the surplus comma at line 8. Others dont care.