【问题标题】:Returning incorrect data返回不正确的数据
【发布时间】:2016-03-26 05:43:09
【问题描述】:

自动表单仅记录一个适用于所有 jobOffers 的接受/拒绝。在 {{#each}} 语句中时,doc=this 不是指单个 jobOffer。

路径:Schema.js

Schemas.Offers = new SimpleSchema({
    offer: {
        type: String,
        optional: true,
        allowedValues: ['Accept', 'Reject'],
        autoform: {
                type: "select-radio",
                options: function () {
                return [
                {label: "Accept", value: 'Accept' },
                {label: "Reject", value: 'Reject' },
                ];
            }
        }
    }
});

路径:template.html

{{#each jobOffers}}

    {{#autoForm collection="Offers" id="offerForm" doc=this type="update" autosave=true}}

        {{> afQuickField name="offer" type="select-radio" template="buttonGroup" label=false}}

    {{/autoForm}}   

{{/each}}

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    循环中的所有表单都使用相同的id='offerForm',它用于确定目标。

    通过向您的 Autoform 添加动态 id 来解决此问题。

    如果 jobOffers 是来自 Mongo 的游标,它将有一个唯一的 _id,您可以使用 id 为表单添加前缀/后缀,例如 id='offerForm{{_id}}'

    【讨论】:

    • 谢谢,我试过{{#autoForm collection="JobOffers" id="offerForm{{_id}}" doc=this type="update"}},但没用。控制台也没有错误。
    • 你能在 DOM/HTML 中检查 id 分配正确吗?
    • 如果我没看错,它会返回id="offerForm{{_id}}"
    • ia 它将 _id 翻译成它的 balue 就是我的意思。它应该是 offerFormGt3usJg86b67 之类的东西..
    • 是的,我认为它实际上是在打印 id="offerForm{{_id}}" 。我需要创建一个助手或钩子来获取_id
    【解决方案2】:

    解决方案是创建一个助手。

    路径:helper.js

    Template.Offer.helpers({
        jobOffers: function () {
            return JobOffers.find({candidateUserId: Meteor.userId()});
        }, 
        makeUniqueID: function () {
          return this._id;
        }
    });
    

    路径:template.html

    {{#each jobOffers}}
        {{#autoForm collection="JobOffers" id=makeUniqueID doc=this type="update"}}
            {{> afQuickField name='offer'}}
            <button type="submit" class="btn btn-primary submit">Update</button>
        {{/autoForm}}   
    {{/each}}
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2013-10-30
      • 2020-07-01
      • 2013-11-19
      • 2018-10-27
      相关资源
      最近更新 更多