-
13 Mar 2012 11:32 AM #1
Answered: Passing data from controller to nested views
Answered: Passing data from controller to nested views
When using the MVC pattern, I'll often have a controller fetch some data, then create a view to display it. But sometimes, the component that needs the data is not the one I created directly, but one of its child components.
What I've been doing is adding a config value for that data on the parent component. Then I'll define a setter for that config (e.g. setMyStore below). The setter will find the child component that needs the data, and pass it along (e.g. setStore below).
My question is: is this a good pattern for passing data to child components? It seems to work, but my concerns are:Code:Ext.define('MyParentComp', { xtype: 'myParentComp', config: { myStore: null }, items: [{ xtype: 'myChildComp' }], setMyStore(myStore) { this.down('myChildComp').setStore(myStore); } }); # in the controller: Ext.Viewport.add({ xtype: 'myParentComp', myStore: Ext.create('MyStore', {...}) });- To find the child component, is it safe to call this.down or this.getComponent from a config setter, like I do in setMyData above? Can I rely on the child components existing when the setter is called?
- When I call the setter on the child component, clearly it's already been created at that point. Is that too late? Obviously this depends on what I'm setting, but do the built-in components typically expect to have all their config values available when they're created? Or is it okay to set them afterwards?
Or does someone have a better pattern to follow?
-
Best Answer Posted by mitchellsimoens
What I would do is hook into the applyItems function, put the data on the item config from there.
-
13 Mar 2012 11:41 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,641
- Vote Rating
- 434
- Answers
- 3107
What I would do is hook into the applyItems function, put the data on the item config from there.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
14 Mar 2012 2:34 AM #3
I don't like the idea (but have also used it) like you of creating a view from within a controller. I think if you can you should pre build and perhaps hide that view from the application startup context. Then use the controller to show this view.
I don't see problems of hitting nested views from the parent controller for the parent view. Its all about controlling the complexity of the code so that it stays readable IMO in this respect. Just think though that those views should just take care of themselves and not know about the controller (and perhaps not even the parent views) but dispatch events that the controller listens for albeit from the parent level.
-
14 Mar 2012 3:18 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,641
- Vote Rating
- 434
- Answers
- 3107
Ok. THen wait for it to be created and have some extra DOM writes affecting performance.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
14 Mar 2012 1:28 PM #5
I'm not sure I agree that controllers shouldn't create views, but that's another discussion.
The applyItems suggestion is a good idea. It's a little harder to maintain than my approach, since you have to know the exact indexes of the items you want to modify (e.g. items[2].items[1]...). But it seems more reliable.
-
15 Mar 2012 11:36 AM #6
On second thought, there may be some issues with doing this in applyItems:
- If you change the "items" argument that's passed to applyItems, it will be changed in all instances of your class, not just the current one.
- I can use applyItems to pass the initial value of the config to my items. But I might also need to look for changes to that config, and update my items with the new values. Normally I'd use updateFoo for that, but updateFoo will also be called when my class is created. So there will be two methods competing to change my items.
I guess I could check for something like this.initialized in my updateFoo method before doing anything -- would that be a safe approach?
- If you change the "items" argument that's passed to applyItems, it will be changed in all instances of your class, not just the current one.
-
15 Mar 2012 11:55 AM #7
applyItems in the Sencha Designer
applyItems in the Sencha Designer
I was reviewing this thread because i'm trying to do essentially the same thing in an app that i'm working on however I'm attempting to use the new designer tools to develop it. i am trying to find the applyItems event in the designer to pass the data through to a child container similar to this. maybe i'm just missing something, but i can't find it in the designer and can't find a way to add it (since the code view is restricted and doesn't allow you to change the code associated with the container itself). has anyone else run into this? what's the appropriate method for passing the data through to child containers when using the designer? Thanks!
-
12 May 2012 6:17 AM #8
I am also trying to work out how to pass data from one container to a child container. Basically split view all that loads container as detailView. Trying to pass data to child container for information and to child button to show video. Any ideas?
-
5 Sep 2012 2:27 PM #9
I'm just trying to get hidden views to resize correctly opon being shown. Landed at least implies that you should be able to hide/show views directly from the controller, and I can make that work, basically by declaring all my views in a class extension of a set controller, but it still doesn't behave like the views that are not hidden.
I keep thinking that the entire point of a library like this, and a program like Architect would be that there would be an easier way.


Reply With Quote