JS Angular构造函数水合物抽象

时间:2016-05-26 23:24:01

标签: javascript angularjs

JS FRAMEWORK / LIBS:

我正在使用Angular 1.5.x和lodash 4.x。

问题:

我是JS的新手,并且在如何将新对象实例注入抽象函数(理想情况为工厂)方面苦苦挣扎。根据我的服务类FormService,我有一个函数创建FormModel(对象)并保持其构造函数 - hydrateModel = function(arr)。无论如何将它抽象到泛型函数或工厂类,以便我可以对象一个新的通用对象实例。

我可以使用后端语言来完成这项工作,但是我很难让这种类型的语法与JS一起工作,我可以为每个数据模型实例(uGly)复制hydrateModel函数。换句话说,我怎样才能做出理想的OR。感谢任何指导。理想情况下,如果有基本答案,这是有道理和道歉的。

编辑 - 理想的结果

会做这样的事情,这显然不起作用。

      hydrateModel = function(arr, varObjectInstance) {
            var newArr = [];
            _.each(arr, function (obj,k) {
               // is it possible in JS to inject a new object and reference it dynamically with a different set of dynamic arguments
                newArr.push( [varObjectInstance](obj, fillable));
            });

            return newArr;
        },

        hydrateModel(arr, new ObjectInstance())

示例,在PHP中,您可以说new $var($arguments)

参考代码背景

// note I've removed the __constructor for brevity sake but it is a fairly basic object extend class

FormModel.$inject = ['__constructor', 'object'];
function FormModel(__constructor, object) {
    function Form(data, keys) {
        __constructor.call(this, data, keys);
    }

    Form.prototype = Object.create(__constructor.prototype);
    Form.prototype.constructor = Form;
    return Form;
}

FormService.$inject = ['FormModel', 'FormDataService'];
function FormService(FormModel,FormDataService) {
    var service = this,
        forms = {},
        fillable = ['app_id','name','class','label','type','maxlength','minlength','placeholder','required','autocomplete','index','helpTitle','helpDescription','messages'],
        hydrateModel = function(arr) {
            var formEloquent = [];
            _.each(arr, function (obj,k) {
                formEloquent.push( new FormModel(obj, fillable));
            });

            return formEloquent;
        };

   // INIT function: 1. get form field (email, name, password,etc) data for 3 forms along with help messages etc.
    service.initForms = function () {
        var self = this;
        _.each({
            register:FormDataService.getRegisterData(), // not including FormDataService for brevity sake but it is  http api service to backend
            contact:FormDataService.getContactData(),
            subscribe:FormDataService.getSubscribeData()
        }, function (obj, key) {
            forms[key] = {
                model:{},
                current:1,
               // below is my question - how could create a function / factory hydrateModel(obj, newGenericObjectInstance) that would be generic so that I can call new [newGenericObjectInstance](arguments) here or in the hydrateModel
                data:hydrateModel(obj),
                view:{},
                state:{},
                help:{}
            }
        });
    };

    return {
        initForms: function () {
            return service.initForms();
        }
    }
}

数据示例

从FormDataService(基本)

返回的表单字段数据行的示例
    var formFieldRowExample = {
        id: '1010',
        name: 'email',
        class: 'form--input',
        label: 'Enter your email',
        type: 'email',
        maxlength: 50,
        minlength: 4,
        placeholder: 'Example: person@example.com',
        required: true,
        autocomplete: 'on',
        validation: [
        {
            type: 'email',
            message: 'Email must be a valid email address'
        },
        {
            type: 'minlength',
            message: 'Your email address is too short'
        },
        {
            type: 'maxlength',
            message: 'Your email address is too long'
        },
        {
            type: 'required',
            message: 'Your email address is required'
        }
    ]
    };

1 个答案:

答案 0 :(得分:0)

在上面的示例中,varObjectInstance是一个对象,而不是构造函数。它无法调用或new编辑。

可行的代码可能如下所示

  hydrateModel = function(arr, Cls) {
    var newArr = [];
    ...
    newArr.push(new Cls(obj, fillable));
    ...
  }

  hydrateModel(arr, ObjectInstance)