介面與資料分離怎麼做?

來源:互聯網
上載者:User

介面與資料分離怎麼做?

 1、介面與資料的分離,必須體現在代碼上,介面的代碼歸介面的代碼,資料的代碼歸資料的代碼,兩者必須涇渭分明。

2、當介面需求發生改變,只需要改寫介面的代碼,並且所改寫的代碼不能影響到資料訪問的代碼。

只有做到這兩者才算介面與資料分離。葉小釵同學讓我上代碼,趁今天還不是很忙,寫下了下面的代碼:

<!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8" />    <title></title>    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>    <script>        $(function () {            var Countries = function () { }                Countries.prototype = {                _items: [],                _getData: function (success) {                    var items = [                       { id: 0, name: '中國' },                       { id: 1, name: '日本' },                       { id: 2, name: '美國' }                    ];                    $.extend(this._items, items);                    success(items);                },                //Events                on_selected: $.Callbacks(),                on_inserted: $.Callbacks(),                //Methods                select: function () {                    var self = this;                    this._getData(function (items) {                        self.on_selected.fire({                            sender: self,                            items: items                        });                    });                },                insert: function (item) {                    var self = this;                    this._items.push(item);                    self.on_inserted.fire({ sender: self, item: item });                }            }                //=======================================================================            // 以下為介面代碼,當要調整介面,改這裡就行啦~~~            var countries = new Countries();            countries.on_selected.add(function (args) {                $(args.items).each(function () {                    $('#countries').append($('<option>').attr('value', this.id).text(this.name));                });            });                countries.on_inserted.add(function (args) {                $('#countries').append($('<option selected="selected">').attr('value', args.item.id).text(args.item.name));            });                var id = 10;            $('#btnAdd').click(function () {                countries.insert({ id: ++id, name: $('#countryName').val() });            });                countries.select();            //=======================================================================        });        </script></head><body>    <select id="countries"></select>    <div>        <input id="countryName" /><button id="btnAdd">添加</button>    </div></body></html>

代碼是可以直接Copy啟動並執行,介面如:

上面的代碼,真正做到了介面與資料的分離:

1、在資料訪問的代碼,沒有一行涉及到UI的,如果有一行,哪怕一行涉及到,都不能算是介面與資料分離。(簡單點說,就是資料訪問的代碼,不能對UI有依賴)

2、當介面需求發生變化,只要修改介面的代碼就可以了。

3、資料訪問的代碼會不會發生變化?一般來說,這個是很少會發生變化的(相比介面)。假如發生變化了,需要修改資料訪問的代碼,所作的修改並不會影響到介面的代碼。

關於代碼,我就不作解釋了,相信有點基礎的同學都能看懂

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.