1. #1
    Sencha User
    Join Date
    Apr 2014
    Posts
    13
    Answers
    1
    Vote Rating
    1
    Txoov is on a distinguished road

      0  

    Default Answered: ViewModel stores help

    Answered: ViewModel stores help


    Hi,

    I'm new to ExtJS 5. I can't figure out how to get the stores in my ViewModel to work properly. I want the stores to get data from a json file. The ajax call is successful because I can see the http request to the users.json file. It must not be loading the data correctly into the stores. If I hard code the stores then it works. BTW, I am binding the data to a grid. I'm sure it's something simple. Thanks..

    Code:
    //MainModel.js file
    Ext.define('sD.view.main.MainModel', {
        extend: 'Ext.app.ViewModel',
    
    
        alias: 'viewmodel.main',
    
    
    /***** defining stores like this works
        stores: {
            data: {
                fields: ['id', 'name', 'email'],
                data: [
                    {id: 101, name: 'Ed',    email: 'ed@sencha.com'},
                    {id: 102, name: 'Tommy', email: 'tommy@sencha.com'}
                ]
            }
        }
    */
    // defining like this doesn't work
        stores: Ext.create('Ext.data.Store', {
            autoLoad: true,
            fields: ['id', 'name', 'email'],
            proxy: {
                type: 'ajax',
                url: '/app/view/main/users.json',
                reader: {
                    type: 'json',
                    rootProperty: 'data',
                    successProperty: 'success'
                }
            }
        })
    });
    
    //users.json file
    {
        success: true,
        data: [
            {id: 101, name: 'Ed',    email: 'ed@sencha.com'},
            {id: 102, name: 'Tommy', email: 'tommy@sencha.com'}
        ]
    }

  2. You need to define it declaratively, like you're doing in the first code block.

    Code:
    stores: {
        myData: {
            autoLoad: true,
            fields: [],
            proxy: {}
        }
    }

  3. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,835
    Answers
    453
    Vote Rating
    609
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      1  

    Default


    You need to define it declaratively, like you're doing in the first code block.

    Code:
    stores: {
        myData: {
            autoLoad: true,
            fields: [],
            proxy: {}
        }
    }
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  4. #3
    Sencha User
    Join Date
    Apr 2014
    Posts
    13
    Answers
    1
    Vote Rating
    1
    Txoov is on a distinguished road

      1  

    Default


    Thanks, evant. The code below works.

    Code:
    Ext.define('sD.view.main.MainModel', {
        extend: 'Ext.app.ViewModel',
    
    
        alias: 'viewmodel.main',
    
    
        stores: {
            grid: {
                storeId: 'gridStore',
                fields: ['id', 'name', 'email'],
                proxy: {
                    type: 'ajax',
                    url: '/app/view/main/users.json',
                    reader: {
                        type: 'json',
                        rootProperty: 'data',
                        successProperty: 'success'
                    }
                },
                autoLoad:true
            }
        }
    });

  5. #4
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,311
    Answers
    13
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    Quote Originally Posted by Txoov View Post
    Thanks, evant. The code below works.

    Code:
    Ext.define('sD.view.main.MainModel', {
        extend: 'Ext.app.ViewModel',
    
    
        alias: 'viewmodel.main',
    
    
        stores: {
            grid: {
                storeId: 'gridStore',
                fields: ['id', 'name', 'email'],
                proxy: {
                    type: 'ajax',
                    url: '/app/view/main/users.json',
                    reader: {
                        type: 'json',
                        rootProperty: 'data',
                        successProperty: 'success'
                    }
                },
                autoLoad:true
            }
        }
    });
    evant or Txoov.

    I have some questions about the above example.

    1. What is the grid property right below stores? How do you use it since you specified storeId?

    2. Do I need to require the store class in the ViewModel? I don't see that you required it.

    3. If my store is a BufferedStore, how do I specify this?

  6. #5
    Ext JS Premium Member
    Join Date
    Jul 2011
    Location
    Atlanta
    Posts
    37
    Answers
    2
    Vote Rating
    6
    Carun is on a distinguished road

      1  

    Default


    #1 - grid is just a name for the store. The name used to reference (bind) this store elsewhere. I don't see a need to specify storeId in this case.

    For example, on the grid this name is used to bind to the store specified in the view model.

    Code:
    {
       xtype: 'grid',
       bind: '{grid}',
       ...
    }
    Grid's default bind property is store, so there is no need to specify the long syntax
    Code:
    bind: { 
       store: '{grid}' 
    }
    #2 - There is no separate store class. It uses the Ext.data.Store to create the instance.

    #3 - I have tried this one yet. There is no buffered property on the store (anymore). I guess specifying the below as part of the store configuration in the view model will create a buffered store.Not sure though.

    Code:
    stores: {
       grid: {
          xclass: 'Ext.data.BufferedStore',
          fields: '', 
          proxy: {}
       }
    }

  7. #6
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,311
    Answers
    13
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    I see, it uses grid as a unique label for binding even though stores have a unique storeId.

    I'm not sure if this is very intuitive, but I appreciate your response.

    I'd assume you need to require the BufferedStore class in the ViewModel class. Did you require it?

  8. #7
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,835
    Answers
    453
    Vote Rating
    609
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Typically you wouldn't use an id for a store here because it's global. View models end up isolating parts of the app.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  9. #8
    Ext JS Premium Member
    Join Date
    Jul 2011
    Location
    Atlanta
    Posts
    37
    Answers
    2
    Vote Rating
    6
    Carun is on a distinguished road

      0  

    Default


    Quote Originally Posted by LesJ View Post
    I see, it uses grid as a unique label for binding even though stores have a unique storeId.
    That's correct. Like Evan mentioned, storeId is only required for global stores. Most stores will be used locally, so there is no need to specify storeId, or to even create separate store classes when using view model. Provide the proxy in the model, and only mention it in the view model, if it has to use a different proxy.

    I'd assume you need to require the BufferedStore class in the ViewModel class. Did you require it?
    You will have to specify it in the requires block if you are using Sencha Cmd.I don't use that to build (load the whole library at startup), so I can get away from specifying it in the requires block, but IMHO it's always a good idea to mention them in the requires block.

  10. #9
    Sencha User
    Join Date
    Jan 2014
    Posts
    6
    Vote Rating
    1
    lemonde is on a distinguished road

      1  

    Default Reuse a class file instead of declaring it again

    Reuse a class file instead of declaring it again


    Is it possible to reuse code from a Store file instead of providing a declarative config for ViewModel's stores? That way we leverage projects standard code organization and avoid repetitions.

  11. #10
    Sencha Premium Member
    Join Date
    May 2011
    Posts
    68
    Vote Rating
    1
    pscanlon1 is on a distinguished road

      0  

    Default


    Quote Originally Posted by lemonde View Post
    Is it possible to reuse code from a Store file instead of providing a declarative config for ViewModel's stores? That way we leverage projects standard code organization and avoid repetitions.

    Are you guys working on this? It seems like a completely reasonable approach.

    IE) I have 2 view models that use the same store.

    Or I dont want to pollute my view with model information. This is a huge change from 4.

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi