summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/mobile/_DataListMixin.js
blob: 025232936dc1f03c13b23448870ae5ff249fb163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//>>built
define("dojox/mobile/_DataListMixin", [
	"dojo/_base/array",
	"dojo/_base/connect",
	"dojo/_base/declare",
	"dojo/_base/lang",
	"dijit/registry",	// registry.byId
	"./ListItem"
], function(array, connect, declare, lang, registry, ListItem){

	// module:
	//		dojox/mobile/_DataListMixin
	// summary:
	//		Mixin for widgets to generate the list items corresponding to the
	//		data provider object.

	return declare("dojox.mobile._DataListMixin", null,{
		// summary:
		//		Mixin for widgets to generate the list items corresponding to
		//		the data provider object.
		// description:
		//		By mixing this class into the widgets, the list item nodes are
		//		generated as the child nodes of the widget and automatically
		//		re-generated whenever the corresponding data items are modified.

		// store: Object
		//		Reference to data provider object
		store: null,

		// query: Object
		//		A query that can be passed to 'store' to initially filter the
		//		items.
		query: null,

		// queryOptions: Object
		//		An optional parameter for the query.
		queryOptions: null,

		buildRendering: function(){
			this.inherited(arguments);
			if(!this.store){ return; }
			var store = this.store;
			this.store = null;
			this.setStore(store, this.query, this.queryOptions);
		},

		setStore: function(store, query, queryOptions){
			// summary:
			//		Sets the store to use with this widget.
			if(store === this.store){ return; }
			this.store = store;
			this.query = query;
			this.queryOptions = queryOptions;
			if(store && store.getFeatures()["dojo.data.api.Notification"]){
				array.forEach(this._conn || [], connect.disconnect);
				this._conn = [
					connect.connect(store, "onSet", this, "onSet"),
					connect.connect(store, "onNew", this, "onNew"),
					connect.connect(store, "onDelete", this, "onDelete")
				];
			}
			this.refresh();
		},

		refresh: function(){
			// summary:
			//		Fetches the data and generates the list items.
			if(!this.store){ return; }
			this.store.fetch({
				query: this.query,
				queryOptions: this.queryOptions,
				onComplete: lang.hitch(this, "onComplete"),
				onError: lang.hitch(this, "onError")
			});
		},

		createListItem: function(/*Object*/item){
			// summary:
			//		Creates a list item widget.
			var attr = {};
			var arr = this.store.getLabelAttributes(item);
			var labelAttr = arr ? arr[0] : null;
			array.forEach(this.store.getAttributes(item), function(name){
				if(name === labelAttr){
					attr["label"] = this.store.getLabel(item);
				}else{
					attr[name] = this.store.getValue(item, name);
				}
			}, this);
			var w = new ListItem(attr);
			item._widgetId = w.id;
			return w;
		},

		generateList: function(/*Array*/items, /*Object*/dataObject){
			// summary:
			//		Given the data, generates a list of items.
			array.forEach(this.getChildren(), function(child){
				child.destroyRecursive();
			});
			array.forEach(items, function(item, index){
				this.addChild(this.createListItem(item));
			}, this);
		},

		onComplete: function(/*Array*/items, /*Object*/request){
			// summary:
			//		An handler that is called after the fetch completes.
			this.generateList(items, request);
		},

		onError: function(/*Object*/errorData, /*Object*/request){
			// summary:
			//		An error handler.
		},

		onSet: function(/*Object*/item, /*String*/attribute, /*Object|Array*/oldValue, /*Object|Array*/newValue){
			//	summary:
			//		See dojo.data.api.Notification.onSet()
		},

		onNew: function(/*Object*/newItem, /*Object?*/parentInfo){
			//	summary:
			//		See dojo.data.api.Notification.onNew()
			this.addChild(this.createListItem(newItem));
		},

		onDelete: function(/*Object*/deletedItem){
			//	summary:
			//		See dojo.data.api.Notification.onDelete()
			registry.byId(deletedItem._widgetId).destroyRecursive();
		}
	});
});