Threaded 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

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