[SOLVED] Trying to add Autoform Hooks into Yogiben:Admin


#1

Hi,

I’m trying to get use yogiben:admin to edit a document containing a JSON-object.
I had this forum post to start with. That describes a solution using the Autoform Hooks. But I don’t have a clue as how to use that with Yogiben:Admin. I tried putting the hook in the schema, like this:

Test.attachSchema new SimpleSchema
autoform:
    before:
        update: (docId, modifier, template) ->
            console.log('arguments')
name:
    type: String
    max: 200
content:
    type: String
    autoform:
        rows: 10

But that does not work.
I also tried setting this up in the AdminConfig;

AdminConfig = {
collections:
{
    Test:{
        tableColumns: [
            {label: 'Name', name: 'name'}
        ],
        routes: {
            edit: {
               onAfterAction: function () { console.log('Im here!'); }
            }
        }
  }
}
}

But that does not work either. So: where to put Hooks on the Autoform that Yogiben uses?

Thanks for any help :slight_smile:
Paul


#2

Hmm… is this maybe not possible? :sob:


#3

Hi,

in case somebody else is wondering: this turns out to be easy but you need to know that Autoform only runs clientside. I was trying to put the Autoform hooks on my server, leading to complaints of Meteor that Autoform was undefined. Because of Meteor’s “all code everywhere” it does not really matter where you put the Autoform.addHooks code, so I created a separate file called autoformHooks.coffe and threw it just in /client/js. Than this code works flawlessly:

AutoForm.addHooks [ 'admin_update' ],
  formToDoc: (doc) ->
    if doc.content?.substring(1) == '{'
      doc.content = JSON.parse(doc.content)
    return doc

  docToForm: (doc) ->
    if typeof doc.content? == 'object'
      doc.content = JSON.stringify(doc.content, null, 2)
    return doc

  formToModifier: (modifier) ->
    if modifier.$set?.content?.substring(1) == '{'
      modifier.$set.content = JSON.parse(modifier.$set.content)
    return modifier
  1. I have a field that can contain both Objects and strings, so that is why I need the check on === ‘object’ etc.
  2. the admin_update is the ID of (all) the forms in Yogiben:Admin, so this will apply to all the forms. Hence the need for the existence-operator ‘?’ to check for the availablility of the content-field.

Just thought it might help someone wrestling with the same problem! :wink:

Paul