Util.js 1.55 KB
define(function(require) {
    'use strict';

    function mergeStatic(startPtr, endPtr, obj, fieldName, filterFunc) {
        var result = filterFunc(obj);
        var ptr = startPtr;
        while (true) {
            result = _.defaults(result, filterFunc(_.result(ptr, fieldName)));
            if (ptr === endPtr) {
                break;
            }
            ptr = ptr.__super__.constructor;
        }
        return result;
    }

    function preventDefault(e) {
        if (e) {
            e.preventDefault();
        }
    }
    return {
        preventDefault: preventDefault,
        stepFalse: function stepFalse(e) {
            preventDefault(e);
            return false;
        },
        getField: function getField(obj, key) {
            return obj[key];
        },
        setField: function getField(obj, key, value) {
            return obj[key] = value;
        },
        getItemKeyAccessor: function getItemKeyAccessor(item) {
            return item.get('key');
        },
        mergeStaticProps: function mergeStaticProps(startPtr, endPtr, obj, fieldName) {
            return mergeStatic(startPtr, endPtr, obj, fieldName, _.identity);
        },
        mergeStaticSets: function mergeStaticSets(startPtr, endPtr, obj, fieldName) {
            return _.keys(mergeStatic(startPtr, endPtr, obj, fieldName, function arrayToMap(array) {
                var result = {};
                _.each(array, function(value) {
                    result[value] = true;
                });
                return result;
            }));
        },
    };
});