跳到主要內容

[快快樂樂教學] YUI2 in 3 使用方法,以calendar widget 為例

YUI2 in 3 使用方法,以calendar widget 為例。

使用方法
Y.use('yui2-calendar', function (Y) {    
    var YAHOO,
        _cal,
        id       = 'calContent',     //指定的id
        pageDate = '9/30/2010',     //設定起始日期, 格式均為,月/日/年
        minDate  = '1/1/2010',        //設定最小日期
        maxDate  = '12/30/2010';    //設定最大日期
        
    YAHOO = Y.YUI2;
    _cal = new YAHOO.widget.Calendar(
        '_cal', 
        id,
        {
            pagedate: pageDate,
            mindate: minDate,
            maxdate: maxDate
        }
    );
    
    //    可以設定多國語系
    _cal.cfg.setProperty("MONTHS_LONG",   ["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]);
    _cal.cfg.setProperty("WEEKDAYS_SHORT", ["S", "M", "D", "M", "D", "F", "S"]);
    
    //    render 事件設定一定要在 render() 執行以前
    _cal.renderEvent.subscribe(_calendarRenderHandler, _cal, true);
    
    //    產生 calendar
    _cal.render();
    
    // 日期選取事件
    _cal.selectEvent.subscribe(_calendarSelectHandler, _cal, true);
    //    改變日期事件
    _cal.changePageEvent.subscribe(_calendarChangeHandler, _cal, true);
});

接下來解釋一下事件。(以下為YUI2 提供樂樂長的列表)

EventFires...Arguments
selectEvent...after a date is selected.Array of date fields representing the dates that were selected. Example: [ [2008,8,1],[2006,8,2] ]
beforeSelectEvent...when a date is selected, before processing the change.N/A
deselectEvent...after a date is deselected.Array of date fields representing the dates that were deselected. Example: [ [2008,8,1],[2006,8,2] ]
beforeDeselectEvent...when a date is deselected, before processing the change.N/A
renderEvent...after the Calendar is rendered.N/A
beforeRenderEvent...just before the Calendar is rendered.N/A
changePageEvent...when the Calendar navigates to a new month (page).The date prior to the Calendar changing pages, and the new date after changing pages
clearEvent...when the Calendar is cleared.N/A
resetEvent...when the Calendar is reset.N/A
beforeHideEvent...just before the Calendar is hidden. Returning false from a subscriber will prevent the Calendar from being hidden.N/A
hideEvent...after the Calendar is hidden.N/A
beforeShowEvent...just before the Calendar is shown. Returning false from a subscriber will stop the Calendar from being shown.N/A
showEvent...after the Calendar is shown.N/A
beforeShowNavEvent...just before the CalendarNavigator is shown. Returning false from a subscriber will stop the CalendarNavigator from being shown.N/A
showNavEvent...after the CalendarNavigator is shown.N/A
beforeHideNavEvent...just before the CalendarNavigator is hidden. Returning false from a subscriber will stop the CalendarNavigator from being hidden.N/A
hideNavEvent...after the CalendarNavigator is hidden.N/A


主要傳送的參數可以參考如下:

選取事件
/*以下為日期選取事件 (when click some date)*/
_calendarSelectHandler = function (type, args, obj) {
    conlose.log(type);    //    目前事件的行為{String}
    var dates = args[0],    //    日期型態物件
        date = dates[0];    //    取得日期資料
        
    conlose.log(date[0], date[1], date[2]);    //輸出資料為 year, month, day
};

切換月份事件
/**
  *    @parm type  {目前行為String}, 
  *          dates {前一頁和目前的日期array},
  *          obj   {calendar 物件}
  */
_calendarChangeHandler = function (typ, dates, obj) {
    var oldDate = new Date(dates[0]),    //前一頁的日期
        newDate = new Date(dates[1]);    //目前的日期
    console.log(newDate.getMonth());
};

calendar執行事件(當月份切換也會執行此事件)
_calendarRenderHandler = function (arg1, arg2, obj) {
    //    arg1, arg2, obj = cal <-指的就是calendar 本身
};

總結:
雖然說大部分事件都是沒有傳入任何參數,不過經過測試,通常第一個參數指的是目前執行的狀態(event status),第三個參數指行事曆物件本身(obj = cal)。

以上的是我自己嘗試的結果,當然還有其他nav, calendar group等強大功能還沒來得及測試,希望如果有心的人可以繼續補足。

留言

這個網誌中的熱門文章

Vibe Coding:到底?氛圍驅動程式開發必殺技?

Vibe Coding(氛圍編程) 是由 OpenAI 共同創辦人 Andrej Karpathy 在 2025 年提出的革命性程式開發方式,它讓開發者透過自然語言與 AI 對話來生成程式碼,徹底改變了傳統的編程模式。 這種開發方式的核心理念是 「順著感覺走」 ,讓 AI 處理技術細節,開發者專注於創意和需求描述。 Vibe Coding 需要基本上的規劃和執行,但並沒有強制規範,從日常經驗來說可分為三個階段, 前期準備、開發過程、和後期維護 三個關鍵階段。每個階段都有其特定的任務和注意事項,正確執行這些步驟將大幅提升開發效率和程式品質。 將靈感與需求透過 AI 快速轉化成產品功能或原型。以下幫你分成 「前、中、後」 三階段要做的事情,適合你自己做、或帶團隊做 前期:設定 vibe & 準備素材 這個階段的重點是 「建立開發語境」 ,因為 AI 的生成表現高度依賴前期提供的上下文與資料。 明確目標 :釐清要解決的問題、預期要做的功能與核心價值。例如在筆記軟體的情境中,可能是:「我要做一款讓使用者能用 Markdown 記錄筆記,並提供標籤與全文搜尋功能的簡單 App。」 收集靈感 :觀察同類產品(如 Obsidian、Notion)、蒐集市場痛點(例如太多筆記軟體無法脫機使用,或同步效能差)。 建立語境 :準備初步 prompt、背景知識、產品定位、品牌調性、目標使用者輪廓等。 確認資源 :決定用哪些工具(Gemini、ChatGPT、設計軟體、流程管理工具等)。 確認完上述內容之後,就可以先開始進行準備規格,進行第一次的 Vibe Coding 方向驗證 提示詞模板準備 很多人會跳過這步驟,但一份 「好的 AI 提示詞模板」 將決定接下來每一次 AI 對話的品質。有效的提示詞模板需具備: 描述具體且無歧義 包含技術要求和約束條件 提供範例資料和測試案例 指定程式碼風格和慣例 例如針對筆記軟體的案例:   「建立一個支援 AI 功能純文字筆記,輸入內容可即時渲染;需支援儲存到本地檔案,提供標籤欄位做分類;以 React 架構,程式風格採用 Tailwind style components 並使用 hooks。」 開發工具選擇 開發工具的選擇 同樣重要,目前市場上主要的 ...

Claude Code Hooks:自動化與安全的最佳實踐

寫在最前頭,這份文章主要寫起來是給自己看, 同時內容是比較適合開發者,工程師們可以做些自動化處理的簡單筆記。 Claude Code hooks Claude Code hooks 是一種強大的自動化機制,允許用戶在 Claude Code 的不同生命週期階段,自定義執行 shell 指令。這種設計讓開發者能夠將規則和自動化行為嵌入到應用層級,確保每次都能可靠執行,而不必依賴 LLM(大型語言模型)是否會選擇執行某項操作。 Hooks 的核心用途 通知 :自訂收到 Claude Code 等待用戶輸入或執行權限時的提醒方式。 自動格式化 :如在每次檔案編輯後自動執行 prettier (針對 .ts 檔)、 gofmt (針對 .go 檔)等。 日誌記錄 :追蹤所有執行過的命令,便於合規或除錯。 自動反饋 :當 Claude Code 產生不符合團隊規範的程式碼時,自動給出反饋。 自訂權限 :阻擋對生產環境檔案或敏感目錄的修改[^1]。 配置與結構 Hooks 透過設定檔進行配置,分為全域( ~/.claude/settings.json )、專案( .claude/settings.json )、本地專案( .claude/settings.local.json )以及企業級策略設定。每個 hook 由「事件名稱」和「匹配器」組成: "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "jq -r '...'" } ] } ] } matcher :用於匹配工具名稱(支援正則表達式),如 Write 、 Edit|Write 、 Notebook.* 。 hooks :當匹配時要執行的命令陣列。 type :目前僅支援 "command" 。 ...

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。 可是在IE7 卻發生了這樣的情況。 Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。