Saturday, April 13, 2013

Javascript deferred promise

The Promise/A proposal defines a method called 'then' that can be used to register callbacks to a promise


Furthermore, a promise can actually end up being in two different states:
  • resolved: in which case data is available
  • rejected: in which case something went wrong and no value is available
Thankfully, the 'then' method accepts two parameters: one for when the promise was resolved, another for when the promise was rejected.
example:
  1. promise.then( function( futureValue ) {
  2.     /* we got a value */
  3. } , function() {
  4.     /* something went wrong */
  5. } );

In the case of certain applications, it is necessary to have several results returned before your application can continue at all (for example, displaying a dynamic set of options on a screen before a user is able to select the option that interests them).Where this is the case, a method called 'when' exists, which can be used to perform some action once all the promises have been fully fulfilled:
  1. when(
  2.     promise1,
  3.     promise2,
  4.     ...
  5. ).then(function( futureValue1, futureValue2, ... ) {
  6.     /* all promises have completed and are resolved */
  7. });
A good example is a scenario where you may have multiple concurrent animations that are being run. Without keeping track of each callback firing on completion, it can be difficult to truly establish once all your animations have finished running. Using promises and 'when' however this is very straight-forward as each of your animations can effectively say ‘we promise to let you know once we're done'. The compounded result of this means it's a trivial process to execute a single callback once the animations are done. For example:
  1. when(
  2.     function(){
  3.         /* animation 1 */
  4.         /* return promise 1 */
  5.     },
  6.     function(){
  7.         /* animation 2 */
  8.         /* return promise 2 */
  9.     }
  10. ).then(function(){
  11.     /* once both animations have completed
  12.        we can then run our additional logic  */
  13. });



reference

Tuesday, January 15, 2013

PHP and MySQL

MYSQL
-----------
find and replace string
- UPDATE table_name SET column = replace(column, 'string to find', 'string to replace');


PHP
-----
basename() - get the file name including extension from path
dirname() - get the path from full bath
strrch(param, param); - find the string after sepecify string (example dot)

Friday, July 22, 2011

mb_strimwidth函数的简单应用

简单的文章截断方法, 支持 Read more 加上 nofollow》一文中所使用的文章截断是使用mb_strimwidth函数来实现的,其实稍微想想就可以发现很多地方都可以使用这个函数,下面说明一下和举一些例子。

先说说mb_strimwidth函数

mb_strimwidth是超轻量级的php函数,用来获取指定的宽度截断字符串。

使用方法

mb_strimwidth ( string $str , int $start , int $width [, string $trimmarker [, string $encoding ]] )

参数说明

$str //指定字符串

$start //指定从何处开始截取

$width //截取文字的宽度

$trimmarker //超过$width数字后显示的字符串

例一、限制文章标题文字个数

如我侧边栏的“Random Posts”和“Recent Posts”,因为侧边栏宽度有限,所以对于长文章标题会出现两行,这样有点影响美观,这时就可以用mb_strimwidth函数来限制文字个数在一行内。

文章标题的调用函数一般是这样:

我主题的侧边栏最多大概显示20个中文,所以可以限制在18个文字内,使用mb_strimwidth函数后变成如下:

其中 36 代表 18 个双字节文字

例二、最新评论的文字个数

上篇文章《带头像显示的最新评论代码 - 蛋疼篇》里面的最新评论长度是用css的overflow:hidden属性来隐藏评论长度的,从而使每条评论占用一行,其实也可以用mb_strimwidth函数来截取固定的文字数量

最新评论代码中评论内容是:strip_tags($rc_comm->comment_content)

用mb_strimwidth函数限制文字变成:mb_strimwidth(strip_tags($rc_comm->comment_content), 0, 36,"...")

很方便的一个函数,还有其他应用就自己举一反三吧,mb_strimwidth()是php的函数,所以不依赖wp的版本。

Saturday, December 4, 2010

What is the debit card limit for CIMB?

If you're using CIMB debit card (mastercard) and you will be asked to enter the "credit card limit" in certain websites. But the limit does not listed in the monthly statement.

So you can just enter "0" (zero) in the "credit card limit field.

----------------------------------------------------------------------------------------------

如果你是使用cimb debit card(mastercard), 在某些购买网站会要求你填入debit card银额限制,

在这里可以填入 "0" (零) 即可

Tuesday, December 8, 2009

Manually IP

Method 1
--------
#system-config-network

Method 2
--------
#system-config-network-tui

Method 3
# cd /etc/sysconfig/network-scripts/ //directory
# nano ifcfg-eth0

//FOR IP ADDRESS AND SUBNET MASK
# Intel Corporation 82573E Gigabit Ethernet Controller (Copper)
DEVICE=eth0
BOOTPROTO=static
DHCPCLASS=
HWADDR=00:30:48:56:A6:2E
IPADDR=10.10.29.66
NETMASK=255.255.255.192
ONBOOT=yes

//FOR GATEWAY
# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=www1.nixcraft.in
GATEWAY=10.10.29.65

//FOR DNS
# vi /etc/resolv.conf
nameserver 10.0.80.11

//RESTART
# /etc/init.d/network restart

Saturday, June 6, 2009

Linux Mount

Command needed
1. create a folder to mount
2. ls -al /dev/disk/by-uuid (to check UUID)
3. sudo ee /etc/fstab (to change mount point)
4. sudo mount -a (mount command)

Informations
-Each "tab" or "space" means end of the command line call.
-use "defaults" in options part for windows partition.
-vfat for FAT and FAT32
-ntfs-3g for NTFS

# /dev/sdb1 (partition comment)
UUID=41baef7a-70fa-4bd0-8ea0-25be9c5ef643 (partition's UUID)
/ (root)
ext3 (file extention)
relatime,errors=remount-ro (options)
0 (0 for windows parition)
1 (0 for windows parition)

Issues
1. Files name with non-unicode
Fixed: UUID=3E40C99340C95277 /home/winD ntfs-3g defaults,utf8 0 0

2. Mounted but unable to write
Fixed: UUID=3E40C99340C95277 /home/winD ntfs-3g defaults,umask=0 0 0

3. Still unable to write after above code used
Fixed: UUID=3E40C99340C95277 /home/winD ntfs-3g defaults,uid=(yourID),gid=(yourID) 0 0

Further Information
- check usable options
- check what is dump and pass