有没有规范的流星.js形成包

is there a canonical meteor.js forms package?

本文关键字:js 流星 有没有      更新时间:2023-09-26

是否有一个表单包被认为是规范的,或者一个可能与最终在核心中结束的任何内容相似的表单包?

在我的搜索中,我根据活动、贯穿性和文档(但可能还有其他)提出了两个主要竞争者:

  • https://github.com/copleykj/Mesosphere
  • https://github.com/aldeed/meteor-autoform

如果有人看过这两个,你能评论一下为什么或在哪里使用一个而不是另一个吗?

由于这个问题还没有得到回答,我将提出一个"为什么你应该自己做"的论点。

表单既是 DOM 的显示,也是验证的显示。 我认为Meteor的这两个工具都足够好,不需要它们之间的另一个抽象。

考虑一下 meteor 给你的东西:你可以为你的对象编写一个类,它本身理解所有的验证和规则。 不是在泛型中,"必须是数字方式",而是以它们存在的方式一样复杂(必须是质数? 您可以编写此类,它可以在客户端和服务器上运行。 应始终在客户端和服务器上进行验证。 验证库如雨后春笋般涌现,因为客户端和服务器(至少)是两种不同的语言。 Node/Meteor 无处不在都是 JS。

为什么不使用这个精彩的功能呢? 不要将验证代码放在多个位置。 将数据提供给对象,让它在客户端(和服务器上)接受或拒绝它。

例如,我通过自己的模板和帮助程序函数的组合来创建每个文本元素:

表格

{{label_text fname='name' title='Agent Name' placeholder='Formal Name' collection='agent'  passthrough='autofocus=autofocus ' }}
{{label_text fname='agentInCharge' title='Agent In Charge' placeholder='Owner' collection='agent'   }}
{{label_text fname='phone' title='Phone Number(s)' placeholder='Include Area  Code'collection='agent'   }}
{{>gps }}

模板

<template name='label_text'>
    <div class="row">
        <label class="large-3" for="{{fname}}_{{_id}}">{{title}}</label>
        <div class="large-8">
            <input name="{{fname}}" 
                   id='{{fname}}_{{_id}}' 
                   class="{{fname}}" 
                   value="{{value}}"
                   data-original="{{value}}" 
                   placeholder="{{placeholder}}" 
                   type="{{type}}" {{passthrough}}  />
        </div>
    </div>
</template>

和一些助手:

generateField = (options) ->
  options.hash.value = options.hash.value or this[options.hash.fname]
  # allow for simple params as default
  options.hash.title = options.hash.title or options.hash.fname
  options.hash.template = options.hash.template or "label_text"
  options.hash.placeholder = options.hash.placeholder or options.hash.title
  options.hash.type = options.hash.type or 'text'
  new Handlebars.SafeString(Template[options.hash.template](options.hash))

Handlebars.registerHelper "label_text", (options) ->
  options.hash.collection = options.hash.collection or 'generic'  
  generateField.call this, options
Handlebars.registerHelper "label_text_area", (options) ->
  options.hash.collection = options.hash.collection or 'generic'
  options.hash.template = "label_text_area"
  options.hash.rows = options.hash.rows or 3
  options.hash.columns = options.hash.columns or 40
  generateField.call this, options
Handlebars.registerHelper "switch", (options) ->
  options.hash.template = "switch"
  options.hash.em = options.hash.em || 7
  generateField.call this, options

然后,我将数据发送到客户端对象,看看它的想法。

目前还没有规范的软件包(Meteor v1.0),似乎这个问题将留给社区(见路线图)。

我还会将joshowens:simple-forms添加到您的列表中,这是一个最小的软件包(因为我喜欢它提供的自由)

我没有尝试过Mesosphere,所以我无法比较,但我认为aldeed:autoform已经获得了更多的牵引力。从github大气明星来看,它是迄今为止最受欢迎的。我相信主要原因是它与集合2(自动插入,更新等)的良好集成。

FWIW,我们使用autoform和collection2。