]> git.proxmox.com Git - extjs.git/blame - extjs/examples/kitchensink/classic/samples/controller/Direct.js
add extjs 6.0.1 sources
[extjs.git] / extjs / examples / kitchensink / classic / samples / controller / Direct.js
CommitLineData
6527f429
DM
1/**\r
2 * This Controller handles initialization requests for the Ext Direct API\r
3 * shared by different examples; in itself it is an example of dynamic\r
4 * API manipulation.\r
5 *\r
6 * The primary reason to have a global Controller handling Ext Direct\r
7 * is to keep Direct configuration centralized for the application,\r
8 * as well as avoid binding transient ViewControlles to Provider\r
9 * instances unless necessary.\r
10 */\r
11\r
12Ext.define('KitchenSink.controller.Direct', {\r
13 extend: 'Ext.app.Controller',\r
14 \r
15 requires: [\r
16 'Ext.direct.RemotingProvider',\r
17 'Ext.direct.Manager',\r
18 'Ext.app.domain.Direct'\r
19 ],\r
20 \r
21 id: 'Direct',\r
22 \r
23 config: {\r
24 /**\r
25 * @cfg {String} apiUrl\r
26 * Default URL to use for Ext Direct service discovery requests.\r
27 */\r
28 apiUrl: 'data/direct/api.php',\r
29 \r
30 /**\r
31 * @cfg {String} [varName="Ext.REMOTING_API"]\r
32 * Default variable name to use for Ext Direct API declaration.\r
33 */\r
34 varName: 'Ext.REMOTING_API',\r
35 \r
36 listen: {\r
37 controller: {\r
38 // We're listening to these events from any Controller.\r
39 // That includes ViewControllers, too.\r
40 '*': {\r
41 directconnect: 'onDirectConnect',\r
42 directdisconnect: 'onDirectDisconnect',\r
43 directgetprovider: 'onDirectGetProvider'\r
44 }\r
45 }\r
46 }\r
47 },\r
48 \r
49 init: function() {\r
50 this.providers = {};\r
51 },\r
52 \r
53 destroy: function() {\r
54 var providers = this.providers,\r
55 url;\r
56 \r
57 for (url in providers) {\r
58 providers[url].disconnect();\r
59 providers[url] = null;\r
60 }\r
61 \r
62 this.callParent();\r
63 },\r
64 \r
65 /**\r
66 * Request remote API from the server, and create a new Direct provider\r
67 * when API declaration is received; alternatively create a new provider\r
68 * if the caller provided a configuration blob for it.\r
69 *\r
70 * @param {Object} options Options to configure Provider request, and to\r
71 * return to the caller in synchronous fashion.\r
72 */\r
73 onDirectConnect: function(options) {\r
74 var me = this,\r
75 providerCfg = options.providerCfg,\r
76 url, provider, request;\r
77 \r
78 url = (providerCfg && providerCfg.url) || options.apiUrl || me.getApiUrl();\r
79 \r
80 // The provider at that URI may have been initialized already\r
81 provider = me.providers[url];\r
82 \r
83 // ViewController may not have specific API URL defined in its\r
84 // configuration, which means we need to return the URL used.\r
85 options.url = url;\r
86 \r
87 if (provider) {\r
88 provider.connect();\r
89 \r
90 options.success = true;\r
91 \r
92 return;\r
93 }\r
94 \r
95 request = Ext.apply({\r
96 url: url,\r
97 varName: options.varName || me.getVarName()\r
98 }, providerCfg);\r
99 \r
100 // We may be passed a provider configuration object that contains\r
101 // sufficient information to create a Provider instance. In that\r
102 // case, loadProvider will add it and fire the callback immediately.\r
103 Ext.direct.Manager.loadProvider(request, me.providerCallback, me);\r
104 },\r
105 \r
106 /**\r
107 * Request to disconnect a Direct provider. This event is fired\r
108 * by ViewControllers being destroyed, to notify Direct controller\r
109 * that they no longer need the specified provider.\r
110 *\r
111 * @param {String} url Service URL for a provider\r
112 */\r
113 onDirectDisconnect: function(url) {\r
114 var provider = this.providers[url];\r
115 \r
116 if (provider) {\r
117 provider.disconnect();\r
118 }\r
119 },\r
120 \r
121 /**\r
122 * Return a provider by its URL to the caller.\r
123 */\r
124 onDirectGetProvider: function(options) {\r
125 options.provider = this.providers[options.url];\r
126 },\r
127 \r
128 providerCallback: function(apiUrl, provider) {\r
129 var me = this;\r
130 \r
131 // Error message can be returned instead of provider\r
132 if (Ext.isString(provider)) {\r
133 me.fireEvent('providerfail', apiUrl, provider);\r
134 \r
135 return;\r
136 }\r
137 \r
138 // We don't need to connect the provider as it was\r
139 // connected automatically by the Direct Manager\r
140 me.providers[apiUrl] = provider;\r
141 \r
142 me.fireEvent('providerinit', apiUrl);\r
143 }\r
144});\r