Scriptaculous 的 sortable callback 與 "fine print"

5 views
Skip to first unread message

lukhnos

unread,
Dec 6, 2006, 4:17:56 AM12/6/06
to hsinchu.js
Scriptaculous (這個詞應該是想和 spectacular "壯觀"
諧音吧)是一套相當方便的 Javascript library,也是 Ruby
on Rails 預先幫 app 安裝的套件之一。

其中一組名為 Sortable
的功能,可以很輕鬆做出例如「可調整順序的清單」或「可調整位置的縮圖預覽」等功能。但是
Sortable 的手冊有一些「小字」(fine
print)要細讀,否則怎麼試都不會出現正確結果。好比說,在
Sortable 說明中:

http://wiki.script.aculo.us/scriptaculous/show/Sortable.create

關於 onUpdate 這個參數,有一句話是這麼說的:

Note that the id attributes of the elements contained in the
Sortable must be named as described in Sortable.serialize

意思是說,當你想要把某一個 element(例如 ul 或
div)裡包含的child elements 變成 sortable
的時候,裡面這些child elements 的 id 必須遵循
Sortable.serialize 的命名規則,例如

foo_1
foo_2
foo_3

然而網頁上沒說的是,callback所收到的,是截掉了 foo
之後的字串部份,例如 [1, 2, 3] ,但是如果你的 id
是這樣命名的

foo_1_some_Data_I_want_to_preserve
foo_2_another_Data_I_want_to_use

結果 callback 收到的陣列,竟然是 [perserve, use]
,而不是 [1_some_Data...., 2_another_Data...]

如果改成這樣,結果就稱心如意得多:

foo_1-some-Data-I-want-to-preserve

等 callback 收到 foo 後面那一大串字後,再用 regex 取出
order 和 data。

簡言之,要命名 sortable element,有一些要注意的 "fine
print"。

Reply all
Reply to author
Forward
0 new messages