Jump To …

read.js

/*global Ply */
/*jshint eqeqeq: true, curly: true, white: true */

The Read module is an abstraction on top of Ajax for loading particular views, or named URLs.

By convention, every view defined has a read method created for it on Ply.read. That URL can be explicitly defined using a __url property on the view, or can be automatically generated by a function assigned to Ply.config.read.urlGenerator.

This module also exists as a namespace for user-defined read functions that are created outside of the automatic view instantiation process. The intention is to avoid explicitly using Ply.ajax with hardcoded URLs. Instead, clients and views should prefer named methods corresponding to the data or view being loaded.

Note that this module makes no assumption about REST, and does not handle requests for any other HTTP verbs. An extension for handling POST and UPDATE requests may come in the future, but has not yet been deemed useful.

Public Methods

Define the read module.

Ply.read = (function () {

    return {

Add

This function is used by clients as well as the UI module to create new read functions. Usage is to pass in a name and a url or optionally, you could pass in an object keyed on names with URLs as values.

        add: function (name, url) {

Check if the first argument is an object.

            if (typeof name === 'object') {

If so, iterate over all own properties.

                for (var n in name) {
                    if (name.hasOwnProperty(n)) {

Call the add method again with the name/url pair.

                        Ply.read.add(n, name[n]);
                    }
                }
                
                return;
            }

Create a method on Ply.read corresponding to the name.

            this[name] = function (data, success, failure) {

Return the result of calling Ply.ajax.request, so the XHR object gets returned to the user. Allows them to call methods of the XHR.

                return Ply.ajax.request({
                    url: url,
                    type: 'GET',
                    data: data
                }, success, failure);

            };
        }

    };

})();

UI