迷你MVVM架構 avalonjs 學習教程7、資料緩衝

來源:互聯網
上載者:User

標籤:des   http   io   java   ar   資料   div   cti   log   

jQuery的許多功能都可以通過avalon的綁定屬性來處理,如click方法對應ms-click,css方法對應ms-css,toggle方法對應ms-visible,它的資料緩衝功能avalon也搬過來了。但不同於jQuery的是,avalon是直接將資料儲存到元素節點的HTML5的data-☆屬性上。在從data-☆屬性還原資料時,它會簡單的資料轉換,再返回給你。

在ms-data綁定中,考慮到如果將對象轉換為字串再還原會丟失函數什麼的,因此它們會直接儲存到元素之上。

<!DOCTYPE html><html>    <head>        <title>TODO supply a title</title>        <meta charset="UTF-8">        <meta name="viewport" content="width=device-width">        <script src="../avalon.js"></script>        <script>            avalon.define({                $id: "test",                $skipArray: ["array", "object"],                number: 111,                number2: NaN,                bool: false,                bool2: true,                nn: null,                vv: void 0,                array: [1, 2, 3],                date: new Date,                object: {                    name: "這是資料"                },                show: function() {                    var elem = avalon(this)                    console.log(                            elem.data("number"), elem.data("number2"),                            elem.data("bool"), elem.data("bool2"),                            elem.data("null"), elem.data("void"),                            elem.data("fn"), this["data-array"],                            this["data-date"], this["data-object"]                            )                }            })        </script>    </head>    <body ms-controller="test">        <div ms-data-number="number"             ms-data-number2="number2"             ms-data-bool="bool"             ms-data-bool2="bool2"             ms-data-void="vv"             ms-data-null="nn"             ms-data-array="array"             ms-data-date="date"             ms-data-object="object"             ms-data-fn="show"             ms-click="show"             >點我</div>    </body></html>

這是一個很簡單的綁定,沒什麼好說的,只要記住對象與數組是直接儲存在元素節點上就行了。下面是它的源碼:

"data": function(val, elem, data) {    var key = "data-" + data.param    if (val && typeof val === "object") {        elem[key] = val    } else {        elem.setAttribute(key, String(val))    }},

迷你MVVM架構 avalonjs 學習教程7、資料緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.