Angular Schema Form - 自定义对象编辑器

时间:2015-02-23 11:50:35

标签: javascript json angularjs jsonschema angular-schema-form

我正在尝试使用angular-schema-form框架创建一个简单的“位置编辑器”。计划是使用可拖动的引脚构建地图编辑器小部件。

项目的json架构相对简单:

location: {
    title: "Location",
    type: "object",
    format: "location",
    properties: {
        latitude: {
            "type": "number"
        },
        longitude: {
            "type": "number"
        }
    }
}

据我所知: http://plnkr.co/edit/tnSdxwrEmRYMNKFzep9f?p=preview

正如您所看到的,编辑名称可以正常工作,但编辑两个位置字段中的任何一个都不会更新模型(除非您清除输入),并且验证仅发生在两个输入元素中的第二个上。 / p>

我不知道接下来要去哪里 - 我找不到对象编辑器的任何其他示例(github上的所有示例都涉及编辑单个'string'项)。

是否支持,如果是,我哪里出错了?

干杯!

1 个答案:

答案 0 :(得分:2)

花了一些时间后,我设法设置对象编辑器来映射子属性。

在编辑器扩展代码中,我遍历模式属性以将它们添加到表单对象,然后我可以在角度html模板中映射它们。

以下是添加了以下内容的js代码:

var location = function(name, schema, options) {
  if (schema.type === 'object' && schema.format === 'location') {
    var f = schemaFormProvider.stdFormObj(name, schema, options);
    f.key  = options.path;
    f.type = 'location';
    options.lookup[sfPathProvider.stringify(options.path)] = f;

    // ADDED THIS BIT:
    // map the schema properties to form properties
    f.properties = {};
    angular.forEach(schema.properties, function(v, k) {
      var path = options.path.slice();
      path.push(k);
      if (options.ignore[sfPathProvider.stringify(path)] !== true) {
        var required = schema.required && schema.required.indexOf(k) !== -1;

        var def = schemaFormProvider.defaultFormDefinition(k, v, {
          path: path,
          required: required || false,
          lookup: options.lookup,
          ignore: options.ignore
        });
        if (def) {
          f.properties[k] = def;
        }
      }
    });

    return f;
  }
};
schemaFormProvider.defaults.object.unshift(location);

和HTML模板(输入类型='数字'而不是'文本')现在看起来像这样:

<div class="form-group" ng-class="{'has-error': hasError()}">

  <label class="control-label" ng-show="showTitle()">{{form.title}}</label>

      <input ng-show="form.properties.latitude.key"
             style="background-color: white"
             type="number"
             class="form-control"
             schema-validate="form.properties.latitude"
             ng-model="$$value$$.latitude" />

      <input ng-show="form.properties.longitude.key"
             style="background-color: white"
             type="number"
             class="form-control"
             schema-validate="form.properties.longitude"
             ng-model="$$value$$.longitude" />

  <span class="help-block">{{ (hasError() && errorMessage(schemaError())) || form.description}}</span>

</div>

我不知道这是否是正确的方法,但它适用于我的目的。