Ext JS - Ext.app.Controller -> Using refs

从 Ext.app.Controller 的 refs 说起

View

Ext.define('MyApp.view.usersView', {
	extend: 'Ext.grid.Panel',
	alias: 'widget.usersView',
	id: 'usersView'
	store: 'usersStore',
	// ...
	dockedItems: [ {
		dock: 'top',
		xtype: 'toolbar',
		autoShow: true,
		enableOverflow: true,
		layout: {
			overflowHandler : 'Menu'// items 溢出处理
		},
		items: [ {
			xtype: 'button',
			text: '搜索',
			margin: '0,0,0,20',
			icon: 'resources/icons/search.png',
			action: 'search'
		}, '-', {
			xtype: 'button',
			text: '刷新',
			margin: '0,0,0,20',
			icon: 'resources/icons/refreshGrid.png',
			handler: function(){
				usersCtrl.refreshGrid();
			}
			// action: 'refreshGrid'
		} ]
	} ]
});

 

Store

Ext.define('MyApp.store.usersStore', {
	extend: 'Ext.data.Store',
	alias: 'widget.usersStore',
	model: 'usersModel',
	listeners: {
		beforeload: function(){
			var view = Ext.getCmp('usersView');
			if(view){
				var p1 = view.down('combobox[name="xxx"]').getValue();
				var p2 = view.down('textfield[name="yyy"]').getValue();
			}
			Ext.apply(this.proxy.extraParams, {
				'param1': p1,
				'param2': p2
			});
		},
		proxy: {
			type: 'ajax',
			url: '',
			reader: {
				type: 'json',
				root: 'zzz',// json 数据格式:{'zzz':{}}
				totalProperty: 'totalCount'
			}
		},
		autoLoad: true
	}
});

 

Controller

Ext.define('MyApp.controller.usersController', {
	extend: 'Ext.app.Controller',
	refs: [{
		ref: 'list',
		selector: 'usersView'
	}],
	views: ['usersView'],
    models: ['usersModel'],
    stores: ['usersStore', 'adminUsersStore'],

    init: function() {
		usersCtrl = this;// 方便在 view、store 上面使用 ctrl 调用方法
        this.control({
			'usersView button[action=search]': {
				click: this.search
			},
			// 如果 view 上,按钮用的是 handler,则这部分代码可以直接省略
			/*'usersView button[action=refreshGrid]': {
				click: this.refreshGrid
			}*/
		});
    },
	// usersView 上的 2 个按钮,刷新与搜索方法
	search: function(){
		this.getUsersStoreStore().load();
	},
	refreshGrid: function(){
		// refs 中 selector 为 usersView 的 ref 为 list,所以 getList() 即可得到 usersView
		this.getList().store.load();// 方式一:grid 获取 store
		this.getList().getStore().load();// 方式二:grid 获取 store
	
		this.getUsersStoreStore().load();// stores: ['usersStore']
	}
});
Controller 下 models: []、views: []、stores: [] 配置项
/**
 * models: []
 */
Ext.define("MyApp.controller.Foo", {
     extend: "Ext.app.Controller",
     models: ['User', 'Vehicle']
 });

This is equivalent of:(等价于)

Ext.define("MyApp.controller.Foo", {
     extend: "Ext.app.Controller",
     requires: ['MyApp.model.User', 'MyApp.model.Vehicle'],

     getUserModel: function() {
         return this.getModel("User");
     },

     getVehicleModel: function() {
         return this.getModel("Vehicle");
     }
 });

// 就是说,controller 的 models 数组配置里,添加了哪些 model,实际上就是将需要的 model 以 requires 做了动态加载,而且同时,还自动生成了 getter 方法。

// 同理,对于 views: [] 和 stores: [],与 models: [] 理解一致。

/*
 * views: []
 */
Ext.define("MyApp.controller.Foo", {
 extend: "Ext.app.Controller",
 views: ['List', 'Detail']
});

This is equivalent of:(等价于)

Ext.define("MyApp.controller.Foo", {
 extend: "Ext.app.Controller",
 requires: ['MyApp.view.List', 'MyApp.view.Detail'],

 getListView: function() {
	 return this.getView("List");
 },

 getDetailView: function() {
	 return this.getView("Detail");
 }
});

/*
 * stores: []
 */
Ext.define("MyApp.controller.Foo", {
 extend: "Ext.app.Controller",
 stores: ['Users', 'Vehicles']
});

This is equivalent to:(等价于)

Ext.define("MyApp.controller.Foo", {
 extend: "Ext.app.Controller",

 requires: [
	 'MyApp.store.Users',
	 'MyApp.store.Vehicles'
 ]

 getUsersStore: function() {
	 return this.getStore("Users");
 },

 getVehiclesStore: function() {
	 return this.getStore("Vehicles");
 }
});

转载于:https://www.cnblogs.com/ikoo4396/p/7380676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值