William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Some coding tips

Some coding tips

Here I list some tips from my coding for quick retrieving:

//Useful in a virtualbox env:
# setsebool httpd_can_network_connect 1
# setsebool httpd_can_network_connect_db 1

//remove from git remote repository:
git rm -r --cached vendor
git rm --cached .gitingore

// document_root/.htaccess file as routers
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteRule ^(.*)$ index.php/$1 [L]

//Leverage Browser Caching
<IfModule mod_expires.c>
# Enable Expirations
ExpiresActive On
# Default Expiration Time
ExpiresDefault "access plus 1 month"
# Expiration for CSS
ExpiresByType text/css "access plus 1 month”
# Expiration for JavaScript
ExpiresByType application/javascript "access plus 1 month"
</IfModule>

// steps required after installing redis-server and mongod
$ sudo groupadd mongod redis
$ sudo useradd -s /sbin/nologin -g mongod -d /var/lib/mongo
      -M -r -c "MongoD daemon" mongod
$ sudo adduser -s /sbin/nologin -g redis -d /var/lib/redis
      -M -r -c "Redis Server" redis

//a typical JS closure: nextId()
var nextId=(function() {
  var counter = 1;
  return function() {
    return counter ++;
  };
}());

// ajax should write like this:
$.ajax({
  type:'POST', url:'',
  data: JSON.stringify(post_data),
  contentType: "application/json; charset=utf-8",
  dataType: "json"
}) //promise
.done(function(data) {}) //then
.done(function(data) {}) //...
.fail(function (jqxhr, textStatus, errorThrown) {})
.always(function (data) {});

// dynamic loading js:
var url="http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js";
$.getScript(url, function() {
  $("#go").click(function(){
    $(".block").animate( { backgroundColor: "pink" }, 1000)
      .delay(500)
      .animate( { backgroundColor: "blue" }, 1000);
  });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: