1. #1
    Sencha User
    Join Date
    Oct 2010
    Posts
    5
    Vote Rating
    0
    mediahack is on a distinguished road

      0  

    Default Local Storage field mapping

    Local Storage field mapping


    I have an AJAX call out to get some info and on return I am getting my store then using setData to apply the results. My mapping does not work. What am I doing wrong?
    Code:
    Ext.define('TC.store.Res',{    extend: 'Ext.data.Store',
        config: {
            storeId: 'res',
            model: 'TC.model.Res',
            autoLoad: true,
            proxy:{
                type: "localstorage",
                id: "res",
                reader: {
                    type: "json",
                    nameProperty: 'mapping'
                }
            }
        }
    });
    Code:
    Ext.define('TC.model.Res',{    extend: 'Ext.data.Model',
        id: "resModel",
        alias: 'model.Res',
        config: {
            fields: [
                { name: "id",         type: "int", mapping: "raw.resid"},
                { name: "status",     type: "string",    mapping: "resstatus"},
                { name: "type",     type: "string", mapping: "restype" },
                { name: "time",     type: "string", mapping: "restime" }        ],
            proxy:{
                type: "localstorage",
                id: "reservation",
                reader:{
                    type: 'json',
                }
            }
        }
    });
    Code:
    Ext.Ajax.request({            url: url,
                method: 'GET',
                params: params,
                withCredentials: true,
                useDefaultXhrHeader: false,
                timeout: 5000,
                headers: { "Content-Type": "application/json" },
                failure: function(response){
    
    
                    Ext.Viewport.unmask();
    
    
                    if (response.timedout) {
                        Ext.Msg.alert('Timeout', "The server timed out :(");
                    } 
                    else if (response.aborted) {
                        Ext.Msg.alert('Aborted', "Looks like you aborted the request");
                    } 
                    else {
                        Ext.Msg.alert('Bad', "Something went wrong with your request");
                    }
                },
                callback: function(options, success, response){
    
    
                    console.log("Got Data");
    
    
                    var data = JSON.parse(response.responseText)[0];
    
    
                    self.applyRes(data, function(){
                        self.getHomeView().setActiveItem(2);    
                    });
    
    
                }
            });
        },
    Code:
    applyReservations: function(json, func){
            var self = this,
                resData = json.data,
                resStore, 
                res;
    
            resStore = Ext.getStore('resStore');
            resStore.setData(resData);
            
            console.log(resData[0]);
            console.log(resStore.data.items[0]);
                    
        },

  2. #2
    Sencha User bt_bruno's Avatar
    Join Date
    Mar 2008
    Location
    Redwood City, CA
    Posts
    154
    Vote Rating
    10
    bt_bruno will become famous soon enough

      0  

    Default


    The JsonReader is the one responsible for reading your data and figuring the mapping out. Since you're using setData() directly, you're bypassing this process.

    If you check method applyData on Store.js on Touch 2.2.1 you'll see this

    Code:
        applyData: function(data) {
            var me = this,
                proxy;
            if (data) {
                proxy = me.getProxy();
                if (proxy instanceof Ext.data.proxy.Memory) {
                    proxy.setData(data);
                    me.load();
                    return;
                } else {
                    
                    ...
    In other words, if your proxy is a Memory proxy, then execute the full flow (proxy->reader->store). If not, it will just update the data collection and fire events.

    As a solution, since you have multiple sources for the store (localstorage or ajax), I'm thinking you could call reader.readRecords(data); to pass your data through the filters and get back a recordset. It needs testing, but the reason it's not working currently is the one explained above.

  3. #3
    Sencha User
    Join Date
    Oct 2010
    Posts
    5
    Vote Rating
    0
    mediahack is on a distinguished road

      0  

    Default


    Bruno, thank you for the response. I'm not sure how to invoke the reader for this store. I get the store and then what?
    Code:
    Ext.define('TC.store.Res',{
    	extend: 'Ext.data.Store',
    	config: {
    		storeId: 'res',
    		model: 'TC.model.Res',
    		autoLoad: false,
    		proxy:{
    			type: "localstorage",
    			id: "res",
    			reader: {
    				type: "json"
    			}
    		}
    	}
    });
    ...
    resStore = Ext.getStore('reservations');

  4. #4
    Sencha User bt_bruno's Avatar
    Join Date
    Mar 2008
    Location
    Redwood City, CA
    Posts
    154
    Vote Rating
    10
    bt_bruno will become famous soon enough

      0  

    Default


    Reader is associated with the Proxy, which is then associated to the store. This should do it:

    Code:
    var recordset = resStore.getProxy().getReader().readRecords(data);
    resStore.add(recordset.getRecords());
    There's a undocumented addData() method on Store that does exactly this:

    Code:
    addData: function(data) {
            var reader = this.getProxy().getReader(),
                resultSet = reader.read(data),
                records = resultSet.getRecords();
    
            this.add(records);
        },

  5. #5
    Sencha User
    Join Date
    Oct 2010
    Posts
    5
    Vote Rating
    0
    mediahack is on a distinguished road

      0  

    Default


    I've been trying to use the addData but get:

    Uncaught TypeError: Cannot call method 'getIdProperty' of undefined

    and google and forums don't provide me a solution/work-around.

  6. #6
    Sencha User bt_bruno's Avatar
    Join Date
    Mar 2008
    Location
    Redwood City, CA
    Posts
    154
    Vote Rating
    10
    bt_bruno will become famous soon enough

      0  

    Default


    Thanks @mediahack. We found a bug WebStorage class (parent of LocalStorage) is overriding updateModel and not calling the parent. As a result, the model doesn't propagate to the reader, and this getIdProperty of undefined occurs.

    Try this patch. You can add on a separate file and require on your app.js, or just append to the top of the file temporarily.

    Code:
    Ext.define('Override.data.proxy.WebStorage', { 
        override: 'Ext.data.proxy.WebStorage',
        updateModel: function(model) {
            if (!this.getId()) {
                this.setId(model.modelName);
            }
            
            Ext.data.proxy.WebStorage.superclass.updateModel.apply(this, arguments);
        }
    });

Thread Participants: 1

Tags for this Thread

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar