获取对自动实例化 Sencha Touch 2 视图的引用
我想从我的 Sencha Touch 2 控制器获取对我的视图的引用,如下所示 (CoffeeScript):
Ext.define 'MyApp.view.Books',
extend: 'Ext.Panel'
id: 'books'
config:
html: 'Hello, Books!'
Ext.define 'MyApp.controller.Books',
extend: 'Ext.app.Controller'
views: [
'Books'
]
init: ->
@control
'#books':
render: ->
console.log 'This is never called. Why?'
show: ->
console.log 'This is called twice.'
# @getBooksView() is not a view instance, it is a class. Why?
console.log 'View class: ' + @getBooksView()?.$isClass
Ext.application
name: 'MyApp'
controllers: [
'Books'
]
launch: ->
Ext.Viewport.setActiveItem
xtype: 'booksview'
出现“Hello, Books”内容(例如,渲染组件),但渲染控制处理程序永远不会出现叫。我在这里做错了什么?
I'd like to get a reference to my view from my Sencha Touch 2 controller, which looks like this (CoffeeScript):
Ext.define 'MyApp.view.Books',
extend: 'Ext.Panel'
id: 'books'
config:
html: 'Hello, Books!'
Ext.define 'MyApp.controller.Books',
extend: 'Ext.app.Controller'
views: [
'Books'
]
init: ->
@control
'#books':
render: ->
console.log 'This is never called. Why?'
show: ->
console.log 'This is called twice.'
# @getBooksView() is not a view instance, it is a class. Why?
console.log 'View class: ' + @getBooksView()?.$isClass
Ext.application
name: 'MyApp'
controllers: [
'Books'
]
launch: ->
Ext.Viewport.setActiveItem
xtype: 'booksview'
The 'Hello, Books' content appears (e.g. the component is rendered), but the render control handler is never even called. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该对任何类使用硬编码的
id
!您应该将视图编码为:注意别名属性。这可以帮助您获取视图实例的引用。当您参考视图时。现在在您的控制器中您有:
请注意,您不是通过 id 属性引用视图,而是通过其别名或 xtype 引用视图。现在,如果您需要访问其他控制器的视图或控制器中的任何其他组件,您还可以使用可用的 refs 系统。在这种情况下,您将在控制器定义中将 refs 数组定义为:
这里,选择器属性是传递给 Ext.ComponentQuery 来获取组件的值。因此,您可以通过以下方式获取 centerpane 的实例:
You shouln't use hardcoded
id
for any of the classes! You should be codding your view as:Note the alias property. This helps you to get the reference of your view's instances. When you refer the view. Now in your controller you have:
Note that you are not refering your view with the id property, instead through its alias or xtype. Now, if you need to access other controller's views or any other component in your controller, you can also make use of the refs system available. In this case, you will define the refs array in the controller definition as:
Here the selector property is the value passed to
Ext.ComponentQuery
to get the component. So, you can get the instance of centerpane by: