var ListApp = (function(){  	var Item = Backbone.View.extend({ 		initialize: function(){ 			this.template = _.template(this.options.itemTpl); 		},  		render: function(){ 	        this.$el.html(this.template(this.model.toJSON())); 	        return this;		 		} 	}), 	List = Backbone.View.extend({ 		events: { 			'click .page-nav': 'pageNav' 		},  		initialize: function(){ 			var self = this; 			this.list = this.$el.find('.app-list'); 			this.page = this.$el.find('.pagination');             this.searchHint = this.$el.find('.list-search-hint');  			if(this.options.ItemView){ 				this.ItemView = this.options.ItemView; 			}  			/*page*/ 			this.pagi = new Rest.model({ 				'page': 1, 				'items': null, 				'update': 0, 				'render': 0, 				'q': '' 			});  			if(this.options.args){ 				this.pagi.set(this.options.args); 			}  			/* collection */ 			this.coll = new Rest.collection; 			this.coll.url = function(){ 				var url = self.options.url + '?p=' + self.pagi.get('page') + '&q=' + self.pagi.get('q'); 				if(self.options.args){ 					for(var key in self.options.args){ 						url = url + '&' + key + '=' + self.options.args[key];  					} 				} 				return url; 			}  			this.coll.parse = function(resp, xhr){ 				self.pagi.set({ 					page: resp.data.page.cur_page, 					items: resp.data.page.items 				}); 				return resp.data.items; 			}  			this.coll.bind('add', this.addItem, this);  			this.pagi.bind('change:render', function(){ 				this.page.html('<ul>' + this.pagi.get('items') + '</ul>'); 			}, this);  			this.progress = this.$el.find('.progress'); 			this.pagi.bind('change:update', function(){ 				while(this.coll.length) this.coll.pop(); 				this.list.empty(); 				this.coll.fetch({add: true, cache: false, success: function(model, data){                     if(self.options.complete){                         self.options.complete();                         }                     self.showHint(); 					self.pagi.set({render: self.pagi.get('render')+1}); 				}, 				beforeSend: function(){ 					self.progress.show(); 				}, 				complete: function(){ 					self.progress.hide(); 				} 				}); 			}, this);  		},          showHint: function(){             var msg, q = this.pagi.get('q');             if(this.searchHint.length < 1) return;             if(q){                 if(!this.coll.length){                    msg = '"' + q + '"没有相关的搜索结果'                        }else{                     msg = '"<b>' + q + '</b>"的搜索结果：'                     }                 this.searchHint.html(msg);             }else{                 this.searchHint.html('');                 }          },  		addItem: function(m){             _this = this;             if(this.options.modelUrl){                 m.url = function(){                     return _this.options.modelUrl + '/' + this.id;                     }                 } 			if(this.ItemView){ 				var view = new this.ItemView({model: m}); 			}else{ 				var view = new Item({model: m, itemTpl: this.options.itemTpl, tagName: this.options.itemTag}); 			} 			this.list.append(view.render().el); 		},  		pageNav: function(e){ 			e.preventDefault(); 			var page = $(e.currentTarget).attr('rel'); 			if(!page || isNaN(page)) return;		 			this.pagi.set({page: page, update: this.pagi.get('update')+1}); 			return false; 		},  		query: function(param){ 			this.pagi.set({page: 1}) 			this.pagi.set({q: param}); 		},  		search: function(param){ 			this.pagi.set({page: 1}) 			_.extend(this.options.args, param); 		} 	});  	return function(params){ 		var app = new List(params), 		handler = { 			app: app, 			setItemView: function(func){ 				app.ItemView = func(); 				return this; 			}, 			pull: function(args){ 				if(args) app.pagi.set(args); 				app.pagi.set({update: app.pagi.get('update')+1}); 				//this.sortBind(); 				return this; 			}, 			query: function(args){ 				app.query(args); 				return this; 			},  			search: function(param){ 				app.search(param); 				return this; 			},  			sortBind: function(){ 				var self = this; 				app.$el.find('.sort-col').each(function(){ 					var by = $(this).attr('sort-by'); 					$(this).addClass('sorting pointer'); 					$(this).click(function(){ 						var	order = $(this).data('sort-order'); 						order =  (order && order == 'asc')?'desc': 'asc'; 						app.$el.find('.sort-col').removeClass('sorting_asc sorting_desc').addClass('sorting'); 						if(by){ 							$(this).addClass('sorting_' + order); 							$(this).data('sort-order', order);                             app.options.args = app.options.args || {}; 							_.extend(app.options.args, {sort: order, sort_by: by}); 							self.pull(); 						}						 					});  				}); 				return this; 			},  			render: function(){ 				params.btn.trigger('click'); 			} 		};  		//将初始加载绑定到特定的按钮上 		if(params.btn){ 			params.btn.click(function(){ 				app.pagi.set({page: 1}); 				handler.pull(); 			}); 		} 		 		return handler; 	}  })();