使用aldeed autoform进行流星更新

时间:2016-02-04 22:35:43

标签: javascript mongodb meteor field

我在收藏品(演出)的单个页面上,我想在' show'上推送到该项目的数组。我有的页面。我的想法是,我有一个弹出下面的模态,你可以添加另一个条目。

我已经使用aldeed autoform尝试了这个,但我没有推动收集项目。为什么呢?

 <!-- Modal Structure -->
  <div id="modal1" class="modal bottom-sheet">
    <div class="container">
    <div class="modal-footer">
      <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">CLOSE</a>
    </div>
    <div class="modal-content">
      {{#autoForm collection="Gigs" id="myForm" }}

      {{#afEachArrayItem name="gear"}}

          {{> afQuickField name=this.current.item}}
          {{> afQuickField name=this.current.description}}

      {{/afEachArrayItem}}
      <button type="submit" class="btn update-gig">Add</button>
    {{/autoForm}}
    </div>

    </div>
  </div>

注意我也尝试过改变js。

Gigs.allow({
  insert: function(userId, doc){
    return !!userId;
  },
  update: function(userId, doc){
    return !!userId;
  }
})

并在js中添加了表单的方法

Meteor.methods({
   addGig: function(id, gear){
     Gigs.update(id,{ $addToSet: { gear: gear } })
   }
});

并在客户端尝试调用它:

Template.Gig.events({
  'submit .update-gig': function(){
    Meteor.call('addGig', this._id, this.gear)
  }
})

简单架构

Gear = new SimpleSchema({
  item: {
    type: String,
    label: "Item",
    optional: false
  },
  description: {
    type: String,
    label: "Description",
    optional: true
  },
  user:{
    type: String,
    label: "User",
    autoValue: function() {
      return this.userId
    },
    autoform: {
      type: "hidden"
    }
  },
  createdAt: {
    type: Date,
    label: "Created At",
    autoValue: function(){
      return new Date()
    },
    autoform: {
      type: "hidden"
    }
  }
});

GigsSchema = new SimpleSchema({
  gig: {
    type: String,
    label: 'Gig Name'
  },
  location: {
    type: String,
    label: 'Location'
  },
  gear: {
    type: [Gear]
  },
  user:{
    type: String,
    label: "User",
    autoValue: function() {
      return this.userId
    },
    autoform: {
      type: "hidden"
    }
  },
  createdAt: {
    type: Date,
    label: "Created At",
    autoValue: function(){
      return new Date()
    },
    autoform: {
      type: "hidden"
    }
  }
});

更新:我有这个工作,但如果有人有一个更简单的方法使用autoform非常感谢!!

客户代码:

'submit .update-gig-form': function(event){

        event.preventDefault();

        var id = FlowRouter.getParam('id');
        var item = event.target.form_item.value;
        var desc = event.target.form_desc.value;

        // jquery materialize css to close modal on page
        $('#modal1').closeModal();

    Meteor.call('addGear', id, item, desc);

    //reset form
    event.target.form_item.value = '';
    event.target.form_desc.value = '';
  }

服务器代码

Meteor.methods({
   addGear: function(id, form_item, form_desc){
     console.log('yay method run');
     Gigs.update(id,{ $addToSet: { gear: {
       item: form_item,
       description: form_desc
     }}
     });
   }
});

0 个答案:

没有答案