1. #1
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      1  

    Default 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).

    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', {...})
    });
    My question is: is this a good pattern for passing data to child components? It seems to work, but my concerns are:
    • 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?

  2. What I would do is hook into the applyItems function, put the data on the item config from there.

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,011
    Answers
    3490
    Vote Rating
    846
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    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
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    255
    Answers
    6
    Vote Rating
    2
    landed is on a distinguished road

      0  

    Default


    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.

  5. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,011
    Answers
    3490
    Vote Rating
    846
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Ok. THen wait for it to be created and have some extra DOM writes affecting performance.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  6. #5
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      0  

    Default


    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.

  7. #6
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      0  

    Default


    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?

  8. #7
    Sencha User
    Join Date
    Mar 2012
    Posts
    1
    Vote Rating
    0
    JBankson is on a distinguished road

      0  

    Default 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!

  9. #8
    Sencha User
    Join Date
    Apr 2012
    Location
    London
    Posts
    17
    Vote Rating
    1
    pbenoit is on a distinguished road

      0  

    Default


    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?

  10. #9
    Sencha User
    Join Date
    Aug 2012
    Posts
    33
    Answers
    2
    Vote Rating
    0
    alane is on a distinguished road

      0  

    Default


    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.