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

2007/11/15

jquery-form

This is our first jquery plugin, and we're very thrilled to release it. jquery-form provides on-the-fly form generation with:
  1. declarative syntax
  2. good default semantic
It's very questionable about why people even need to generate a whole form all in javascript, and my saying is that this is one kind of future. It looks like this when you use it:
$("#form1").form({
    legend: "Quiz #4",
    params: {
        "Title": "What's the answer ?",
        "your_answer": {
            label: "Your Answer",
            value: "42"
        }
    }
})
.find(":submit").val("Save").end()
.submit(function() {
    $("#message").html("You just submit that form");

    setTimeout(function() { $("#message").empty(); }, 5000);

    return false;
});
At this moment you can only see text fields generated, and it's not very customizable yet. More and more type of traditional fields, or smart fields, are being integrated into this branch. So stay tuned. Please visit it's official page for more details: http://code.handlino.com/wiki/jquery-form

2007/6/8

Function.intervalize released

Function.intervalize 已經釋出於 JSAN。也可於和多下載區下載 Function-intervalize-0.01.tar.gz。此為 JSAN 的標準散佈檔,解開之後原始碼位於 lib/Function/intervalize.js,可直接以 script 標籤的方法載入。

這個模組是為了解決此項特定的問題:函式可能常常會在一段很短暫的時間內被大量呼叫好幾次,使得 CPU 使用率突然飆高。比如說,如果我有一函式 notify():

function notify(msg) { // 在畫面左下角顯示一小段訊息

}
其達成功能,是在畫面某處顯示訊息(如 MSN 的上線通知),並於一小段時間後消失。基於 Javascript 多頭執行的特性,這個函式可能會在多處被呼叫,甚至,有可能在同一秒內被呼叫數十次(MSN 的聯絡人的確也有可能突然在同一秒內有數人同時上線)。那麼,可以想像的是,顯示的提醒訊息將會突然蓋滿畫面而失去其原本的功用。

此時的解決辨法,便是固定讓它每秒出現一則就好。使用 Function.intervallize 的話,則改用以下的寫法便可:

var notify = (function(msg) { // 在畫面左下角顯示一小段訊息

}).intervalize(1000);
如此一來,即便是連續呼叫 notify 五次:

notify("foo"); notify("bar"); notify("baz"); notify("foobar"); notify("foobaz");

也不會一次出現五則,而是在五秒鐘內,每秒出現一則。intervalize() 方法的參數便是區間的毫秒數。在此時間區間之內,原函式的功能保證只會進行一次。

(本文同時 Cross Post 於 hsinchu.js)

Cheers, Kang-min Liu

註:openjsan.org 的 Index 目前尚未更新,靜待一、二日便可見到下載的連結。

2007/5/12

Javascript localization

eIt may sound pretty ridiculous that we're doing l10n in Javascript, but it's unavoidably required. Since Javascript plays more important role in View code in virtually all web frameworks. Rails has RJS, which basically require Javascript to perform in-place page-updating job. Jifty also use Javascript to update page fragments. Jemplate is a full javascript template library. Extjs is a full-js UI builder. In these libraries, more or less, we need to put string literals in or Javascript code.

So we've done our way localization strings in Javascript: Asynapse.Localization. It's now put in our Asynapse project Subversion repository. After it's initialized, you call this function: _, yes, a function named just an underscore character, which is basically the standard name of the function that accomplished this purpose in lightweight languages. Therefore, we think it's totally OK to named it just underscore. Besides, it doesn't conflict with prototype.js, so that won't make people sad.

It's almost as simple as this:

  # Initialize
Asynapse.Localization.init({
"lang": "zh_TW",
"dict_path": "/javascripts/loc"
})

# use it
alert( _("Nihao") )

We use JSON as our dictionary format. It looks like this:

{
 "Nihao": "你好",
 "Good bye": "再見"
}

But wait, that doesn't feel any easier. Writing JSON by hand is actually worse then editing anything else, isn't it ? Besides, it doesn't quite fit our Asynapse concept.

As we allAsian people know, translation strings in code is something we never want to repeatedly do. So we figured out a way to make it less repeating: We choose to cover .po files to JSON.

If you're already developing some projects that has l10n support, you probably use gettext to the real l10n task. Then you definitely know what .po files are. They are the dictionaries in the format defined in gettext standard. po2json.pl is the script that convert from po dictionaries into JSON files which can be loaded by Asynapse.Localization library. That means, you're simply reusing dictionaries that you already had. If you have new strings from Javascript code, just add them manually into your po file, and they'll also be reused in server side code too.

That now feel more Asynapse, we wins at both client and server side coding.

So far this library is lying in the Subversion repository, stay tuned, we'll soon release it to several major code archiving sites once it's OK enough to be released. For developers who can't wait, just checkout our code from google svn code. Definitely poke us, give us some feed back if you like it (or hate it)