Quantcast
Channel: SmartClient Forums
Viewing all articles
Browse latest Browse all 4756

Common practice for data sources

$
0
0
What is the "preferred" way of creating data sources (I'm using SmartClient 10) in general? So far I'm creating a BaseDataSource class, extending the default RestDataSource class and for each entity I subclass this class to set the field configuration and data URL. After that I create a global instance via an ID. In a lot of examples I see only the usage of the global instance (so for all places in an application using a customers data source, it seems like the same instance is used. I guess this is done to have automatic data binding, but I wonder if there's downsides to this.

So this is what I have:

Code:

    // create a base class that sets up properties to talk with a RubyOnRails back-end
    isc.defineClass('BaseRestDataSource', isc.RestDataSource).addProperties({
        dataFormat: 'json',
        operationBindings: [
            { operationType: 'fetch', requestProperties: { httpMethod: 'GET' } },
            { operationType: 'add', requestProperties: { httpMethod: 'POST' } },
            { operationType: 'update', requestProperties: { httpMethod: 'POST', params: { '_method': 'PUT' } } },
            { operationType: 'remove', requestProperties: { httpMethod: 'POST', params: { '_method': 'DELETE' } } }
        ],
        sendMetaData: false,
        transformRequest: function (request) {
            if (request.operationType === 'fetch') {
                // check if paging is enabled
                if ((request.startRow !== undefined) && (request.endRow !== undefined)) {
                    request.params = isc.addProperties({}, request.params, {
                        offset: request.startRow,
                        limit: request.endRow - request.startRow
                    });
                }
            } else {
                request.data = isc.addProperties({}, request.data, {
                    authenticity_token: METADATA.authenticity_token
                });
            }
            return this.Super('transformRequest', arguments);
        }
    });

    // create a entity specific subclass
    isc.defineClass('CustomersDataSource', isc.BaseRestDataSource).addProperties({
        dataURL: '/api/customers',
        fields: [
            ...
        ]
    });

    // create a global instance
    isc.CustomersDataSource.create({
        ID: 'customers'
    });

So, I might remove the creation of the entity specific subclass and create instances instead of them (thus combining the last two steps). I don't really like the idea of working with the IDs to create global instances, but it does help in creating simpler code.

Viewing all articles
Browse latest Browse all 4756

Trending Articles