Results 1 to 3 of 3

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sencha User ujin's Avatar
    Join Date
    Jul 2011
    Western Russia
    Vote Rating

    Lightbulb 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:

        "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:

        "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 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:

    Ext.regStore('FoosStore', {
        model: 'Foo',
        proxy: {
            type: 'rest',
            autoAppendParams: true,
            appendId: true,
            url: '',
            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

Tags for this Thread

Posting Permissions

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