Hybrid View

  1. #1
    Sencha User ujin's Avatar
    Join Date
    Jul 2011
    Location
    Western Russia
    Posts
    2
    Vote Rating
    0
    ujin is on a distinguished road

      0  

    Lightbulb RestProxy — need a different 'root' for different actions, is it possible?

    RestProxy — need a different 'root' for different actions, is it possible?


    Greetings. I have a simple RestProxy which I should adapt to RESTful, yet quirky server-side API. Some responses has 'root', some not, and I'm trying to wrap it with a single proxy.

    NB: Code of FoosStore provided below for reference — nothing special there, though.

    What works right:

    FooStore's proxy expects JSON responce to have 'results' array in it (see Proxe code down below, root property of the reader). Proxy works as expected works when server returns JSONed set of Foos (they're wrapped in 'results' array).

    `GET /foos`, and server respond with properly wrapped one, and it gets processed nicely:

    Code:
    {
        "total_entries": 2,
        "skip": 0,
        "limit": 20,
        "results": [{
            "id": "1234",
            "property" : "value"
            "created_at": "2011-08-04T15:22:20Z",
            "updated_at": "2011-08-04T15:22:20Z",
        },
        { /* another one */ }
        ]
    }
    What's wrong?

    Whenever I request a single Foo, server responds with bare JSON representation, without 'results' wrapper. Proxy expects to find a wrapper array, so it fails with «TypeError: 'undefined' is not an object (evaluating 'root.length')».

    `POST /foos/1234`, and proxy breaks. This one is no good:

    Code:
    {
        "id": "1234",
        "property" : "value"
        "created_at": "2011-08-04T15:22:20Z",
        "updated_at": "2011-08-04T15:22:20Z",
    }
    Real-life example

    I've updated some stored Foos, then asked FooStore to sync with server. A batch of 'update' operation is formed. Proxy chokes on first one, because it's 'POST /foos/123' — and server predictably responds with JSON of unwrapped Foo.

    What i tried:

    1. Changing server-side API is not an option.
    2. Reader is oblivious to type of action which caused server response it is processing, so all responses are treated equally.
    3. I think i should extend Ext.data.Reader() and override something, but I have no idea what exactly should I do — I'm new to Sencha Touch and framework's architecture is a bit obscure to me.

    Ideas and guidance are much appreciated, for I'm stuck. :)

    ======

    Below is the code of FooStore:

    Code:
    Ext.regStore('FoosStore', {
        model: 'Foo',
        proxy: {
            type: 'rest',
            autoAppendParams: true,
            appendId: true,
            url: 'http://foobar.com/foos',
            actionMethods: {
                create: 'POST',        
                read: 'GET',            
                update: 'POST', // was PUT
                destroy: 'DELETE' 
            },
            api: {
                create : 'create',
                read   : 'read',
                update : 'update',
                destroy: 'destroy'
            },
            reader: {
                type: 'json',
                root: 'results'
            }
        }
    });
    Last edited by ujin; 4 Aug 2011 at 11:49 AM. Reason: typo

  2. #2
    Sencha User
    Join Date
    Jul 2010
    Location
    Switzerland
    Posts
    78
    Vote Rating
    0
    fx-mike is on a distinguished road

      0  

    Default


    I've had a similar problem, where I got some weird xml from the server. I solved it by using custom readers to bring all responses into a single format that I could then save to the store.

    This is roughly how the reader could look like:
    Code:
    app.readers.SpecialReader = Ext.extend(Ext.data.XmlReader, {
    
    	readRecords: function(doc) {
    		if (Ext.isArray(doc)) {
    			doc = doc[0];
    		}
    
    		//do your stuff, e.g. (in pseudocode)
    		if !doc.results
    			doc = {
    				"total_entries": 1,
    				"skip": 0,
    				"limit": 20,
     				"results": [doc]
    			}
    
    		return app.readers.SpecialReader.superclass.readRecords.call(this, doc);
    	}
    });
    
    Ext.data.ReaderMgr.registerType('SpecialReader', app.readers.SpecialReader);
    This will look very similar for JSON I suppose, just have a look at what the JSON reader normally does in readRecords and go from there.

  3. #3
    Sencha User ujin's Avatar
    Join Date
    Jul 2011
    Location
    Western Russia
    Posts
    2
    Vote Rating
    0
    ujin is on a distinguished road

      0  

    Lightbulb This is it!

    This is it!


    Quote Originally Posted by fx-mike View Post
    I've had a similar problem, where I got some weird xml from the server. I solved it by using custom readers to bring all responses into a single format that I could then save to the store. <…> This will look very similar for JSON I suppose, just have a look at what the JSON reader normally does in readRecords and go from there.
    Thank you for pointing in the right direction — looks like sench-y way to solve the problem.

    (...Aha, that's how you extend from built-in readers. Forgot about ReaderMgr completely, thank you for reminding. :)

    Now I'll adapt it to json, it's all similar, more or less.

Thread Participants: 1

Tags for this Thread

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