在控制器中设置存储事件的侦听器
我有一个带有商店、模型和一些视图的控制器。
我需要在控制器中监听 store 的 beforesync
和 write
事件,但我不知道如何在控制器 control< 中设置这些监听器/代码>-函数。
我的商店看起来像这样:
Ext.define('DT.store.UsersStore', {
extend : 'Ext.data.Store',
model : 'DT.model.User',
id : 'myStore'
autoSync : true,
proxy : {
type : 'ajax',
api : {
read : '/load_entries',
update : '/update_entry'
},
reader : {
type : 'json',
root : 'user',
successProperty : 'success'
}
}
});
现在我尝试监听控制器中的事件:
...
init : function () {
this.control({
'myStore' : {
beforesync : this.doSomething,
write : this.doSomethingElse
}
});
},
...
我的预期结果是当事件被触发时,函数将被执行。 但此时,当他们被解雇时,什么也没有发生。
我怎样才能让它发挥作用?
I have a controller with a store, a model, and some views.
I need to listen for the beforesync
and write
event of the store in the controller, but I don't know how to set these listeners in the controllers control
-function.
My store looks like this :
Ext.define('DT.store.UsersStore', {
extend : 'Ext.data.Store',
model : 'DT.model.User',
id : 'myStore'
autoSync : true,
proxy : {
type : 'ajax',
api : {
read : '/load_entries',
update : '/update_entry'
},
reader : {
type : 'json',
root : 'user',
successProperty : 'success'
}
}
});
Now I try to listen to the events in my controller :
...
init : function () {
this.control({
'myStore' : {
beforesync : this.doSomething,
write : this.doSomethingElse
}
});
},
...
My expected result is that the functions will be executed, when the events are fired.
But at this time nothing happens when they are fired.
How can I get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你的方式是可能的,但它是不理想,海事组织。更好的方法是使用控制器的存储 getter。在你的情况下,代码将是这样的:
Your way is possible but it's not ideal, IMO. The better way is to use controllers's store getter. In your case the code would be something like this:
这是分子人的答案的替代语法。
而不是写
你可以写
我认为这个替代定义读起来更好一点,
。我从Izhaki给我的答案中得到了这个。
Here is an alternative syntax to Molecular Man's answer.
Instead of writing,
You can write
I think this alternative definition reads a little bit better.
I took this from an answer Izhaki gave me.
对于 Extjs 4.2.1,如果您使用“storeId”而不是 id,使用“listen”函数而不是“control”,那么访问存储侦听器的初始方法实际上是有效的:
As for Extjs 4.2.1, your initial way of accessing the store listener would actually work, if you were using the 'storeId' instead of the id and the 'listen' function instead of 'control':
App - 您的应用程序对象
onBeforeSync 函数您可以在控制器中实现它...这是我可以将事件分配给存储并仍然在控制器中实现逻辑的唯一方法。我希望它有帮助
App - your application object
The function onBeforeSync you can implement it in the controller ... this is the only way i could assign the event to the store and still implement the logic in the controll. I hope it helps
我自己解决了。
我在面板的
render
事件中手动添加了侦听器,谢谢您的帮助@nscrob。
I solved it by myself.
I added the listener manually in the
render
-event of my PanelThank you for the help @nscrob.
希望这一添加能够帮助人们避免花费一些时间来调试库核心:
与使用控制器的 getter 方法访问 Controller 内的 Ext.data.Store 实例的实践相关:例如上面的“DT.store.UsersStore”使用 this.getUsersStoreStore():
请注意,如果存储已经在视图中关联(例如,被声明为“UsersGrid”Grid.panel.Panel 小部件定义的存储属性),则此 getter 方法将在事实上,同一 Store 类的另一个实例,而不是小部件使用的实例!
原因是在构造函数配置对象中添加存储,如下所示:
实际上会在 Ext.data.StoreManager.map 哈希中添加一个新的存储实例 - 假设“UsersStore”是迄今为止实例化的唯一存储对象 - 映射键现在看起来像:
现在假设您想使用商店代理读取一些新数据并在“UsersGrid”中显示这些新数据,并且您希望在用户单击某些内容时执行所有这些操作,因此在控制器内您将有一个处理程序用户事件的方法代码:
获取引用的调用将在 this.getStore('UsersStore') 内部进行翻译,并将返回对生成的控制器的引用 - 1:“UsersStore” - 而不是 - 2:“myStore” - 就像人们可能的那样预期的。此外, load() 调用将使用新模型加载 UsersStore 实例,这不会反映在您的网格视图中(因为网格已绑定并侦听“myStore”存储实例生成的事件)。
因此,更好地使用通用 getStore 方法通过其 itemId 访问商店: this.getStore('storeItemId')
Hope this addition will help someone out there to avoid spending some hours on debugging the library core:
Related to this practice of accessing Ext.data.Store instance inside Controller using the controller's getter method: e.g. for the "DT.store.UsersStore" above using this.getUsersStoreStore():
pay attention that if the store is already associated in a view(e.g. was declared as the store property for a "UsersGrid" Grid.panel.Panel widget definition) then this getter method will retrieve in fact another instance of the same Store class and not the instance used by the widget!
The reason is that adding the store in the constructor configuration object like this:
will in fact add a new store instance in Ext.data.StoreManager.map hash so - supposing that 'UsersStore' is the only Store object instantiated so far - the map keys now look like:
Now imagine you want to read some new data using your store'proxy and display this new data in the "UsersGrid" and you want to do all these when user clicks on something, so inside controller you will have a handler method for the user event with the code:
That call to get the reference will be translated internally in this.getStore('UsersStore') and will return a reference to the controller generated - 1: "UsersStore" - and not - 2: "myStore" - as one might expected. Furthermore, the load() call will load the UsersStore instance with the new Models and this will not be reflected in your grid view(because the grid is bound and listens to the events generated by "myStore" store instance).
So better access the store by its itemId using the general getStore method: this.getStore('storeItemId')
为什么不直接转发商店的活动呢?例如:
this.getUsersGrid().relayEvents(this.getUsersStoreStore(), ['write'], 'store')
然后能够
this.control('somegrid-selector ': {storeWrite: function(){...}})
Why not just relay the store's events? For example:
this.getUsersGrid().relayEvents(this.getUsersStoreStore(), ['write'], 'store')
And then be able to
this.control('somegrid-selector': {storeWrite: function(){...}})
以防万一有人绊倒这个。
ExtJS 7.3
Just in case somebody stumbels over this.
ExtJS 7.3