Hybrid View
-
14 Jun 2012 7:11 PM #1
Problem with localstorage store and model with hasMany association
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:my store with localstorage proxy: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' } });I tried to add record to store:Code:Ext.define('Cartstore', { extend: 'Ext.data.Store', config: { model: 'Cart', proxy: { type: 'localstorage', id: 'carts' } } });After that I see in localstorage first model data only without associated data.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();
No errors in console. But no associated data in localstorage.
Where I am wrong?
-
15 Jun 2012 12:18 AM #2
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:
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.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.Developer/Alien Technologies at Mobile Ambitions Aps, Denmark.
-
15 Jun 2012 6:21 AM #3
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.
-
15 Jun 2012 9:55 AM #4
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.
-
17 Jun 2012 9:34 AM #5Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 433
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.
-
17 Jun 2012 2:39 PM #6
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):
and what is the result? I can store and read stored data with hasMany association.Code:associations: [ { type: 'hasMany', model: 'Product', associationKey: 'products', foreignKey: 'cart_id', name: 'products', autoLoad: true, store: { model: 'Product', proxy: { type: 'localstorage', id: 'products' } } } ]
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.


Reply With Quote