Update with autoform

Hello there !

I’m actually trying to edit a record of a specific collection in a project. I’m using aldeed:autoform for this and i really want to go through it.

Perhaps, the update doesn’t work (Nothing happen on submit).

Here is my code :

collection.js

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';

import SimpleSchema from 'simpl-schema';

Pipelines = new Mongo.Collection('pipelines');

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

PipelinesSchema = new SimpleSchema ({
    name: {
        type: String,
        label: "Name"
    },
    desc: {
        type: String,
        label: "Description"
    }
});

Meteor.methods({
    crmDeletePipeline: function(id) {
        Pipelines.remove(id)
    },
});

Pipelines.attachSchema(PipelinesSchema);

crm_single_pipeline.js

Template.crmSinglePipeline.onCreated(function() {
    var self = this;
    this.editMode = new ReactiveVar(false);
    self.autorun(function() {
        var id = FlowRouter.getParam('_id');
        self.subscribe('Pipelines', id);
    });
});

Template.crmSinglePipeline.helpers({
    pipeline: () => {
        var id = FlowRouter.getParam('_id');
        return Pipelines.findOne({_id: id});
    },
    updatePipelineId: function() {
        return FlowRouter.getParam('_id');
    },
    editMode: function () {
        return Template.instance().editMode.get();
    },
});

Template.crmSinglePipeline.events({
    'click .btn-danger': function (){
        var id = FlowRouter.getParam('_id');
        Meteor.call('crmDeletePipeline', id);
        FlowRouter.go('crm');
        swal("Deleted", "This record was properly deleted !", "success");
    },
    'click .btn-warning': function (event, template){
        template.editMode.set(!template.editMode.get());
        console.log(template.editMode.get());
    },
});

crm_single_pipeline.html

<template name="crmSinglePipeline">
        <div class="row col-md-12">
            <div class="col-md-8">
                <h1>Pipeline</h1>
            </div>
            <div class="col-md-4">
                <div class="pull-right">
                    <button type="button" class="btn btn-danger">Delete</button>
                    <button type="button" class="btn btn-warning">Edit</button>
                </div>
            </div>
        </div>
        <hr>
        {{#if editMode}}
            {{> quickForm collection="Pipelines" doc=this id="updatePipelineId" type="update" class="new-pipelines-form" autosave=true}}
        {{else}}
            <div class="row col-md-12">
                <div class="col-md-6">
                    <h3>{{pipeline.name}}</h3>
                </div>
            </div>
        {{/if}}
</template>

I actually can’t figure out what’s wrong in it. Also, i think i don’t really get what the doc=this use to find out which document should be updated.

Thanks a lot for your help in advance :slight_smile:

Brawcks

I will assume that:

  1. You published your collection on server
  2. You imported your collection in crm_single_pipeline.js

What router package are you using?

doc=this would be a paramater sent via URL, like
http://localhost:3000/pipeline/edit/xxxxxxxxx

where xxxxx would be your pipeline Id. These should be treated on your JS as well.

Hi :slight_smile: Thanks for your time,

First, yes i’ve published my collection on my server, and i subscribed aswell to it on client.

I’m actually using FlowRouter, here are the packages you want :

kadira:flow-router # Routing
kadira:blaze-layout # Blase js
arillo:flow-router-helpers # Flow router helpers

Here is my routes file configuration :

FlowRouter.route('/crm/pipeline/:_id', {
    name: 'crm/pipeline',
    action() {
        // IT RENDER THE MAIN TEMPLATE, AND USE A VARIABLE TO LOAD A MODULE TEMPLATE INSIDE
        BlazeLayout.render('mainTemplate', {module: 'crmSinglePipeline', sidebar: 'sideNavbarcrm'});
    }
});

Thanks for your support :slight_smile:

No problem,
There is more than one way on doing this. I followed this discussion and chose this method, that seemed more explicit and coherent.

  1. Subscription at template level:
Template.crmSinglePipeline.onCreated(function(){
 var self = this;
 this.autorun(function(){
  var _id = FlowRouter.getParam('_id');
  self.subscribe('aSinglePipeline, _id);
};
};
  1. Our Template Helper
Template.crmSinglePipeline.helpers({
 getPipeline: function(){
  var pipeId = FlowRouter.getParam('_id');
  var myPipeline = Meteor.Pipelines.findOne(_id);
  return myPipeline;
}
});
  1. finally, use the helper on the template.
{{> with getPipeline}}
{{> quickForm collection="Pipelines" doc=this id="updatePipelineId" type="update" class="new-pipelines-form" autosave=true}}
{{/with}}

This should work. But you check the link above to see all the discussion and how people waits for the collection to be loaded before Blaze renders the autoform.

1 Like

Hi, sorry for the late answer, i was on a trip !

I’ll be looking there to solve it, and be back if needed, thanks a lot for all your explanations ! :slight_smile:

1 Like