1

我正在尝试使用 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

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

我不确定下一步该往哪里看——我找不到对象编辑器的任何其他示例(github 上的所有示例都涉及编辑单个“字符串”项)。

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

干杯!

4

1 回答 1

2

After spending some more time, I've managed to set up the object editor to map the child properties.

In the editor extension code, I traverse the schema properties to add them to the form object, and then I can map them in the angular html template.

Here's the js code with the additions:

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);

and the HTML template (with the inputs type='number' rather than 'text') now looks like this:

<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>

I don't know if this is the correct way to do it but it works for my purposes.

于 2015-02-23T21:36:16.933 回答