All files / sp-property-table ViewModel.js

91.67% Statements 11/12
100% Branches 4/4
80% Functions 4/5
91.67% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                                                                                                  1x 1x                         1x 1x                                                                               3x 1x   2x     2x 2x     2x 2x    
import DefineMap from 'can-define/map/map';
import FieldIteratorMap from '../util/field/base/FieldIteratorMap';
 
/**
 *  A `<sp-property-table />` component's ViewModel. This viewmodel
 * extends the [util/field/ ]'s properties
 * @class ViewModel
 * @memberof sp-property-table
 *
 */
export default FieldIteratorMap.extend('PropertyTable', {
    /** @lends sp-property-table.ViewModel.prototype */
    /**
    * A string referencing a field property that will exclude that field
    * from this classes fields. The default is 'detail'.
    * @type {String}
    * @memberof sp-property-table.ViewModel.prototype
    */
    excludeFieldKey: {
        default: 'details'
    },
    /**
     * A flag to allow editing (Not yet implemented)
     * TODO: implement editing
     * @type {Boolean}
     * @memberof sp-property-table.ViewModel.prototype
     */
    edit: {
        type: 'boolean',
        default: true
    },
    /**
     * A flag to allow deleting (Not yet implemented)
     * TODO: implement deleting
     * @type {Boolean}
     * @memberof sp-property-table.ViewModel.prototype
     */
    delete: {
        type: 'boolean',
        default: true
    },
    /**
     * The ID value of the object that should be retrieved. This value along with the connection object will be used to retrieve an object from a RESTful service
     * @type {Number}
     * @memberof sp-property-table.ViewModel.prototype
     */
    objectId: {
        type: 'number',
        set (id) {
            this.fetchObject(this.connection, id);
            return id;
        }
    },
    /**
     * The connection object that should be used to retrieve an object. This
     * value along with the objectId value will be used to retrieve an object
     * from a RESTful service
     * @link http://canjs.com/doc/can-connect.html can-connect
     * @type {can-connect}
     * @memberof sp-property-table.ViewModel.prototype
     */
    connection: {
        set (con) {
            this.fetchObject(con, this.objectId);
            return con;
        }
    },
    /**
     * A generic object to display in a tabular format. This can be used instead
     * of providing a connection and objectId property
     * @type {Object}
     * @memberof sp-property-table.ViewModel.prototype
     */
    object: DefineMap,
    /**
     * A promise that resolves to the object. Used to determine state of current fetching operations
     * @type {Promise}
     * @memberof sp-property-table.ViewModel.prototype
     */
    objectPromise: {},
    /**
     * Header names mapped to an object.
     * ```
     * field: 'Field',
     * value: 'Value'
     * ```
     * @type {Object}
     */
    headers: {
        default () {
            return {
                field: 'Field',
                value: 'Value'
            };
        }
    },
    /**
     * Asynchronously fetches an object using a can-connect model and an id
     * @see [can-connect](https://connect.canjs.com/) for futher information
     * @param  {can-connect} con The connection object to an api resource. The model must have a `get(id)` method.
     * @param  {Number} id  The id number of the object to retrieve
     * @return {Promise}     A promise that is resolved once the object is retreived
     */
    fetchObject (con, id) {
        if (!con || !id) {
            return null;
        }
        const def = con.get({
            id: id
        });
        def.then((obj) => {
            this.object = obj;
        });
 
        this.objectPromise = def;
        return def;
    }
});