原創|使用教程|編輯:龔雪|2013-11-15 10:43:52.000|閱讀 1366 次
概述:Kendo UI Web包含數百個創建HTML5 web app的必備元素,包括UI組件、數據源、驗證、一個MVVM框架、主題、模板等。在前面的文章《HTML5 Web app開發工具Kendo UI Web教程:創建自定義組件(二)》中,對在Kendo UI Web中如何創建自定義組件作出了一些基礎講解,下面將繼續前面的內容。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Kendo UI Web包含數百個創建HTML5 web app的必備元素,包括UI組件、數據源、驗證、一個MVVM框架、主題、模板等。
在前面的文章《HTML5 Web app開發工具Kendo UI Web教程:創建自定義組件(二)》中,對在Kendo UI Web中如何創建自定義組件作出了一些基礎講解,下面將繼續前面的內容。
使用一個數據源
現在如果想要實現一個數據源組件或是MVVM aware模式,需要再執行一些其他的步驟。 在下面將會創建一個DataSource aware組件,要使DataSource aware有數據源,首先需要在DataSource基礎對象上使用create convenience方法。
創建或初始化數據源:
that.dataSource = kendo.data.DataSource.create(that.options.dataSource);
這一行代碼主要是為你的組件數據源提供了比較靈活的方式,這個樣子你就不用創建一個新的數據源來綁定到組件上。
數據源作為數組:
$("#div").kendoRepeater({ dataSource: ["Item 1", "Item 2", "Item 3"] });
如果你傳遞一個簡單的數組, kendo.data.DataSource.create方法將會為你創建一個新的基于數組數據的DataSource,并返回到that.dataSource。你也可以通過指定它內嵌的配置值來新建一個datasource。
將數據源作為配置對象:
$("#div").kendoRepeater({ dataSource: { transport: { read: { url: "//mydomain/customers" } } } });
這里正在設置數據源的配置,但是并沒有創建一個實例。這個kendo.data.DataSource.create(that.options.dataSource)將會獲得這個配置對象,并用指定的配置返回一個新的DataSource實例。現在已經提供了相同的靈活性,在我們自己的組件中盡可能多的方式對repeater組件指定DataSource。
Handling Events:
接下來需要做的就是綁定到DataSource change事件并處理它。在這里你將會基于從DataSource讀取的數據改變你的DOM。通常可以用一個刷新的方法。一般都會公用這個刷新的方法,主要由于在初始化后,你和其他人可能會在組件或某個節點上調用這個高性能。
綁定到Change Event:
// bind to the change event to refresh the widget that.dataSource.bind("change", function() { that.refresh(); });
有Change Event的組件:
(function($) { var kendo = window.kendo, ui = kendo.ui, Widget = ui.Widget, CHANGE = "change"; var Repeater = kendo.ui.Widget.extend({ init: function(element, options) { var that = this; kendo.ui.Widget.fn.init.call(that, element, options); // initialize or create dataSource that._dataSource(); }, options: { name: "Repeater" }, _dataSource: function() { var that = this; // returns the datasource OR creates one if using array or configuration that.dataSource = kendo.data.DataSource.create(that.option.dataSource); // bind to the change event to refresh the widget that.dataSource.bind(CHANGE, function() { that.refresh(); }); } }); kendo.ui.plugin(Repeater); })(jQuery);
現在需要向_dataSource方法添加一些東西,如果需要的話將會從DataSource中提取。通過檢查that.options的自動綁定配置值可以實現。接下來是調用that.dataSource.fetch(),需要注意的是,這個fetch和read是不同的,如果這個DataSource還沒有被讀取,fetch會只會填充DataSource。如果在組件被初始化之前,在DataSource上read已經被調用了的話,那么DataSource就不會再次被讀取了。
_dataSource: function() { var that = this; // returns the datasource OR creates one if using array or configuration that.dataSource = kendo.data.DataSource.create(that.options.dataSource); // bind to the change event to refresh the widget that.dataSource.bind(CHANGE, function() { that.refresh(); }); // trigger a read on the dataSource if one hasn't happened yet if (that.options.autoBind) { that.dataSource.fetch(); } }
這個自動綁定配置選項還不存在,現在需要添加它到組件上的選項對象中,并賦予一個默認的值為true。在默認的情況下,Kendo UI中所有的DataBound組件都會自動綁定。
為選項添加AutoBind:
options: { name: "Repeater", autoBind: true }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件