顯示具有 Javascript 標籤的文章。 顯示所有文章
顯示具有 Javascript 標籤的文章。 顯示所有文章

2015年5月22日 星期五

[NodeJs] Solution of Redis runnig out of memory when using Kue (使用 Kue 時 Redis 記憶體空間不足解法)



將碰到的情況整理分享,下次碰到可以少花些時間到處爬。



與 Redis 相關的環境

1. Sails Session 存放在 Redis。
2. Kue (處理 job queue 的 node module) 使用 Redis。



事由

這次正式機發生 Redis 的記憶體空間不足,
導致使用者無法登入,因為 Session 無法存入 Redis。

錯誤訊息
ERR command not allowed when used memory > 'maxmemory'


2014年11月24日 星期一

[AngularJS] Directive Matching (屬性名稱辨識)



正規化處理程序

1. 把 element 或者 attribute 開頭的 x- 和 data- 都去掉。
2. 把用「:」、「-」、「_」隔開的名稱合在一起變成 camelCase 的形式。

範例

<div ng-controller="Controller">
  Hello <input ng-model='name'> <hr/>
  <span ng-bind="name"></span> <br/>
  <span ng:bind="name"></span> <br/>
  <span ng_bind="name"></span> <br/>
  <span data-ng-bind="name"></span> <br/>
  <span x-ng-bind="name"></span> <br/>
</div>
上面的每一行同樣都是使用 ngBind 的意思。


2014年10月27日 星期一

[Coffeescript] Difference between with and without question mark in condition statements (判斷式中變數加不加問號的差別)



一般問號使用的時機為,如果有某物件則判斷某物件屬性 ... 等,像是
if obj?.value
  console.log "obj exists, obj.value exists, obj.value isn't 0 and obj.value isn't ''"


這邊則討論較為特別的使用情況:

console.log "obj.value? is true" if obj.value?
console.log "obj.value is true" if obj.value

以上兩行敘述,主要的差別在 obj.value 為 0 或者 "" 時會表現不一樣,

也就是情況一和二:

### Condition 1
obj =
  name: "aaa"
  value: 0

# obj.value? is true
### Condition 2
obj =
  name: "aaa"
  value: ""

# obj.value? is true

以下是兩者表現相同的情況:

### Condition 3
obj =
  name: "aaa"

# NO OUTPUT.
### Condition 4
obj =
  name: "aaa"
  value: null

# NO OUTPUT.
### Condition 5
obj =
  name: "aaa"
  value: {}

## obj.value? is true
## obj.value is true
### Condition 6
obj =
  name: "aaa"
  value: []

## obj.value? is true
## obj.value is true

轉成 Javascript 後比較清楚,分別為:

// if obj.value?
if(typeof value !== "undefined" && obj.value !== null) {
    console.log("obj.value? is true");
}

// if obj.value
if(obj.value) {
  console.log("obj.value is true");
}

2014年10月17日 星期五

[Javascript] Best way to store key-value array (存 key-value 陣列的最佳方法) / Object v.s. Array tips (物件 v.s. 陣列 小撇步)


資料型態簡單釋義

物件 (Object)

可以存放屬性和函式的容器。

陣列 (Array)

可以存放多個不一定要相同資料型態變數的容器。

當兩種型態皆可使用時,如何選擇?

2014年10月11日 星期六

[Javascript] MapReduce 用法 (MapReduece Usage)



[].map(callback [, thisArg])

把 A 型態陣列 map 成 B 型態陣列,
回傳的 B 型態陣列長度相同,B 有可能等同於 A。
var callback = function(element, index, context) { /* omitted */ }
第二個參數 thisArg 用來指定 callback 中 this 指到的物件。

2014年9月18日 星期四

[AngularJS] Scope issues when using custom directive (用客製 directive 的 scope 注意事項)



當使用 AngularJS 客製化的 HTML 標籤 (Directive) 時,
要特別注意 Scope,下面敘述五種不同設定下的 Scope 差別。

預備知識

scope 和 transclude 的預設值皆為 false。

2014年8月19日 星期二

[Javascript] Q (promise, defer) usage (Q 用法)



整理分享簡單用法。

可以參考 q 原始碼網頁 。

[NodeJs] npm package.json dependencies version (npm package 版本寫法整理)

version       Must match version exactly
>version     Must be greater than version
>=version   etc
<version
<=version
~version     "Approximately equivalent to version"
^version     "Compatible with version"
1.2.x           1.2.0, 1.2.1, etc., but not 1.3.0
http://...       See 'URLs as Dependencies' below
*                 Matches any version
""               (just an empty string) Same as *
version1 - version2         Same as >=version1 <=version2.
range1 || range2              Passes if either range1 or range2 are satisfied
git...            See 'Git URLs as Dependencies' below
user/repo    See 'GitHub URLs' below