[Javascript] Handlebars precompiling templates

ref. http://net.tutsplus.com/tutorials/javascript-ajax/introduction-to-handlebars/
http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro

== Handlebars precompiling templates

1. download the Node.js package through npm :
sudo npm install handlebars -g

2. create a file for your template and compile it like so: -m: minimize output
app/assets/javascripts> mkdir templates
vim hello.handlebars
<p>Hello {{name}}</p>

handlebars ./templates/ -f ./templates/templates.js -m

3. simply include the output file along with the run-time version(handlebars.runtime.js) of Handlebars (you can use the regular version, but it’s larger) and type the following:

vim application.js
//= require handlebars-1.0.rc.1.min
//= require ./templates/templates
or
//= require andlebars.runtime
//= require ./templates/templates

 

Source Code:

var template = Handlebars.templates['hello'];
var html = template({ name: ‘World’ });

Mixing precompiling and runtime compiling templates

Handlebars.getTemplate = function(name) {
	if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
		$.ajax({
			url : 'templates/' + name + '.handlebars',
			success : function(data) {
				if (Handlebars.templates === undefined) {
					Handlebars.templates = {};
				}
				Handlebars.templates[name] = Handlebars.compile(data);
			},
			async : false
		});
	}
	return Handlebars.templates[name];
};

[Javascript] underscorejs _.all(), _.any() methods

Determine whether all of the elements match a truth test. Delegates toECMAScript 5‘s native every if available. Aliased as all.
  _.every = _.all = function(obj, iterator, context) {
    iterator || (iterator = _.identity);
    var result = true;
    if (obj == null) return result;
    if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
    each(obj, function(value, index, list) {
      if (!(result = result && iterator.call(context, value, index, list))) return breaker;
    });
    return !!result;
  };
Determine if at least one element in the object matches a truth test. Delegates to ECMAScript 5‘s native some if available. Aliased as any.
  var any = _.some = _.any = function(obj, iterator, context) {
    iterator || (iterator = _.identity);
    var result = false;
    if (obj == null) return result;
    if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
    each(obj, function(value, index, list) {
      if (result || (result = iterator.call(context, value, index, list))) return breaker;
    });
    return !!result;
  };

 

[Javascript] Array.prototype.slice.call( arguments, 0 )

ref. https://shanetomlinson.com/2010/converting-arguments-to-a-javascript-array/

Convert arguments to an array of javascript

var testFunction = function() {  
  // Create a new array from the contents of arguments  
  var args = Array.prototype.slice.call(arguments);  
  
  var a = args.shift();  
  console.log("The first argument is: %s", a);  
  
  // send the remaining arguments to some other function  
  someOtherFunction(args);  
};  

[jQuery]jQuery.each( object, callback ) break or continue

ref. http://docs.jquery.com/Utilities/jQuery.each#objectcallback

If you wish to break the each() loop at a particular iteration you can do so by making your function return false. Returning non-false(true) is the same as a continue statement in a for loop, it will skip immediately to the next iteration.

 

var arr = [ "one", "two", "three", "four", "five" ];
$.each(arr, function(i) {
    if(arr[i] == 'three') {
        return true; //continue
    }
    alert(arr[i]);
});

==> alert one, two, four, five
$.each(arr, function(i) {
    if(arr[i] == 'three') {
        return false; //break
    }
    alert(arr[i]);
});

==> alert one, two

[ubuntu] avconv: 비디오와 오디오 컨버터

avconv is a very fast video and audio converter that can also grab from
a live audio/video source. It can also convert between arbitrary sample
rates and resize video on the fly with a high quality polyphase filter.

ref. http://manpages.ubuntu.com/manpages/precise/man1/avconv.1.html

avconv 컨버터를 사용하려면  libav-tools 패키지를 설치한다.

$ sudo  apt-get install  libav-tools

EBS Radio 교육방송 녹음하기위한 쉡스크립트

#!/bin/bash
RADIO_ADDR="rtmp://ebsandroid.nefficient.com/fmradiofamilypc/familypc1m"
RADIO_NAME="ebs_radio"

PROGRAM_NAME=$1
RECORD_MINS=$(($2 * 60))
DEST_DIR=$3

REC_DATE=`date +%Y%m%d-%H%M`
TEMP_FLV=`mktemp -u`

MP3_FILE_NAME=$PROGRAM_NAME"_"$REC_DATE.mp3

rtmpdump -r $RADIO_ADDR -B $RECORD_MINS -o $TEMP_FLV
avconv -i $TEMP_FLV -ac 2 -ab 128 -vn -y  -f mp3 $MP3_FILE_NAME

rm $TEMP_FLV

mkdir -p $DEST_DIR
mv $MP3_FILE_NAME $DEST_DIR

[Paperclip]Attach non-image files in Rails3

ref. Attach Non Image Files in Rails With Paperclip:
http://practiceovertheory.com/blog/2009/05/19/attach-non-image-files-in-rails-with-paperclip/

이미지가 아닌 문서 파일을 업로드할 경우 Paperclip gem 패키지에서 에러가 발생함.
before_post_process 메소드를 이용해 처리한다.

class Attachment < ActiveRecord::Base
  attr_accessible :asset
  #paperclip: file attachments
  has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
                     :default_url => "/images/:style/missing.png",
                     #:url => "/system/:class/:attachment/:id_partition/:style/:filename",
                     #:url => "/system/:class/:attachment/:id/:style_:hash.:extension",
                     #:hash_secret => "rmdwjddmlgla"
                     :url => "/system/:class/:attachment/:id/:style/:updated_at.:extension"

  validates :asset, :attachment_presence => true
  before_post_process :image?
  
  def image?
    !(asset_content_type =~ /^image.*/).nil?
  end
  
end

[Ubuntu]Recover MongoDB Data following Unexpected Shutdown

ref. http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/

log file:
$ tail -f /var/log/mongodb/mongodb.log

mongodb data path:
/var/lib/mongodb/

- 2가지 방법 중에 2번째 방법으로 간단히 복구
1. mongod.lock 파일 삭제:
$ sudo rm /var/lib/mongodb/mongod.lock

2. mongod 를 –repair 옵션으로 복구 시작:
$ sudo mongod –dbpath /var/lib/mongodb –repair
$ sudo chown -R mongodb:mongodb /var/lib/mongodb

3. mongodb 시작:
$ sudo service mongodb start
$ sudo service mongodb status

 

[ubuntu 12.04] reinstall grub

ubuntu server 12.04 를 부팅할 때,
grub prompt 상태에 들어가서
grub> _

더 이상 진행되지 않아 grub를 다시 설치해 문제를 해결했다.

ubuntu CD로 부팅해 복구시스템 모드로 들어가
grub를 설치함.
fdisk 로 부팅 파티션을 확인(*표시: /dev/sda1 *)

# sudo fdisk -l

# sudo mount /dev/sda1 /mnt
# sudo grub-install --boot-directory=/mnt/  /dev/sda

# sudo reboot