Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom proxies on Stores and Models seems inconsistent (and does not work on Models)

Am using Extjs 4, and have created a custom Rest Proxy to handle communication with my Zend backend api. (See post http://techfrere.blogspot.com/2011/08/linking-extjs4-to-zend-using-rest.html)

When using a Store to handle communication, I was using Ext.require to load the proxy, and then referenced the proxy on the type field and all was good and it loaded my data: as per:

Ext.require('App.utils.ZendRest');
...
proxy   : {
    type  : 'zest',              // My custom proxy alias
    url   : '/admin/user'
    ...
}

I then decided to try to use the proxy directly on a model... and no luck. The above logic does not work.
Problems
1. When referencing zest, it does not find the previously loaded ZendRest class (aliased to proxy.zest)
2. It tries to load the missing class from App.proxy.zest (which did not exist.)
So I tried moving my class to this location and renaming to what it seemed to want. No luck.
It loads the class, but still does not initialize the app... I get no errors anywhere so v difficult to figure out where the problem is after this...

For now it seems I will have to revert to using my Zend Rest proxy always via the Store.

Question is... has anyone else seen the behavior? Is it a bug, or am I missing something?

Thanks...

like image 585
MarkOfSine Avatar asked Aug 06 '11 13:08

MarkOfSine


1 Answers

Using your proxy definition, I've managed to make it work. I am not sure why it doesn't work for you. I have only moved ZendRest to Prj.proxy namespace and added requires: ['Prj.proxy.ZendRest'] to the model.

Code:

// controller/Primary.js
Ext.define('Prj.controller.Primary', {
    extend: 'Ext.app.Controller',
    stores: ['Articles'],
    models: ['Article'],
    views: ['article.Grid']
});

// model/Article.js
Ext.define('Prj.model.Article', {
    extend: 'Ext.data.Model',
    fields: [
        'title', 'author', {
            name: 'pubDate',
            type: 'date'
        }, 'link', 'description', 'content'
    ],
    requires: ['Prj.proxy.ZendRest'],
    proxy: {
        type: 'zest',
        url: 'feed-proxy.php'
    }
});


// store/Articles.js
Ext.define('Prj.store.Articles', {
    extend: 'Ext.data.Store',
    autoLoad: true,
    model: 'Prj.model.Article'
});

// proxy/ZendRest.js
Ext.define('Prj.proxy.ZendRest', {
    extend: 'Ext.data.proxy.Ajax',
    alias : 'proxy.zest',
    appendId: true,
    batchActions: false,
    buildUrl: function(request) {
        var me        = this,
            operation = request.operation,
            records   = operation.records || [],
            record    = records[0],
            format    = me.format,
            reqParams = request.params,
            url       = me.getUrl(request),
            id        = record ? record.getId() : operation.id;

        if (me.appendId && id) {
            if (!url.match(/\/$/)) {
                url += '/';
            }

            url += 'id/' + id;
        }

        if (format) {
            reqParams['format'] = format;
        }
        /* <for example purpose> */
        //request.url = url;
        /* </for example purpose> */
        return me.callParent(arguments);
    }

}, function() {
    Ext.apply(this.prototype, {
        actionMethods: {
            create : 'POST',
            read   : 'GET',
            update : 'PUT',
            destroy: 'DELETE'
        },
        /* <for example purpose> */
        reader: {
            type: 'xml',
            record: 'item'
        }
        /* </for example purpose> */
    });
});

Here is working sample, and here zipped code.

like image 186
Krzysztof Avatar answered Oct 01 '22 03:10

Krzysztof