Results 1 to 10 of 15

Thread: Problem with localstorage store and model with hasMany association

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    176
    Vote Rating
    20
      0  

    Default Problem with localstorage store and model with hasMany association

    Is store with 'localstorage' proxy is support a models with hasMany associations?
    I have a model with one hasMany association:
    Code:
    Ext.define('Cart', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'id', type: 'string'},
                {name: 'created', type: 'date'}
                {name: 'status', type: 'boolean'}
            ],
            idProperty: 'id',
            identifier: {
                type: 'uuid'
            },
            associations: [
                {
                    type: 'hasMany',
                    model: 'Product',
                    associationKey: 'products',
                    foreignKey: 'cart_id',
                    name: 'products',
                    autoLoad: true
                }
            ]
        }
    });
    
    Ext.define('Product', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'product_id', type: 'string'},
                {name: 'quantity', type: 'integer'},
                {name: 'cart_id', type: 'string'},
            ],
            idProperty: 'product_id',
            belongsTo: 'Cart'
        }
    });
    my store with localstorage proxy:
    Code:
    Ext.define('Cartstore', {
        extend: 'Ext.data.Store',
        config: {
            model: 'Cart',
            proxy: {
                type: 'localstorage',
                id: 'carts'
            }
        }
    });
    I tried to add record to store:
    Code:
    var newCart = Ext.create('Cart', {
                'created': new Date(),
                'status': true
            });
            newCart.products().add(Ext.create('Product', {
                'product_id': 10,
                'quantity': 1
            }));
    
            Ext.getStore('Cartstore').add(newCart).sync();
    After that I see in localstorage first model data only without associated data.
    No errors in console. But no associated data in localstorage.
    Where I am wrong?

  2. #2
    Sencha User rohdef's Avatar
    Join Date
    Mar 2010
    Location
    Aarhus, Denmark
    Posts
    67
    Vote Rating
    5
      0  

    Default

    I think you should define a localstorage for your products and use the foreign key to select the right products when loading. I think the reason for you problem is found in the documentation, since localstorage says:

    HTML5 localStorage is a key-value store (e.g. cannot save complex objects like JSON), so LocalStorageProxy automatically serializes and deserializes data when saving and retrieving it.
    You might also have luck in configuring the store and/or writer, but I think having them in separate stores will be better. I've been programming with nested stores since yesterday (due to nested data in my JSON service) and I have to say I don't think it's pretty compared to normal store setup.
    Developer/Alien Technologies at Mobile Ambitions Aps, Denmark.

  3. #3
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    176
    Vote Rating
    20
      0  

    Default

    Make two separate stores for a cart and products - it's a good idea.
    But I do not see any barriers for the storage of models with hasMany association because data is stored as single JSON-string.
    Most likely this is error in localstorage proxy... or this proxy simply not support hasMany associations.
    If so - then let the developers will confirm this.

  4. #4
    Sencha User rohdef's Avatar
    Join Date
    Mar 2010
    Location
    Aarhus, Denmark
    Posts
    67
    Vote Rating
    5
      0  

    Default

    I don't know if you have seen it, but this seems to answer your question: http://www.sencha.com/forum/showthre...l=1#post762500
    Developer/Alien Technologies at Mobile Ambitions Aps, Denmark.

  5. #5
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,553
    Vote Rating
    1272
      0  

    Default

    Quote Originally Posted by rohdef View Post
    I don't know if you have seen it, but this seems to answer your question: http://www.sencha.com/forum/showthre...l=1#post762500
    It's not currently supported but something Tommy is working on how best to do it, it's actually harder than it seems.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  6. #6
    Sencha User kostysh's Avatar
    Join Date
    Nov 2011
    Location
    Odessa, Ukraine
    Posts
    176
    Vote Rating
    20
      2  

    Default

    for now my problem was fixed by adding a store configuration into hasMany association config (http://docs.sencha.com/touch/2-0/#!/...ny-cfg-store):
    Code:
    associations: [            {
                    type: 'hasMany',
                    model: 'Product',
                    associationKey: 'products',
                    foreignKey: 'cart_id',
                    name: 'products',
                    autoLoad: true,
                    store: {
                        model: 'Product',
                        proxy: {
                            type: 'localstorage',
                            id: 'products'
                        }
                    }
                }
            ]
    and what is the result? I can store and read stored data with hasMany association.
    As it turned, all included

    I have not yet quite completely done tests about possibility of working with hasMany data. If I have any problems - I'll write here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •