2015-07-16 14 views
6

hoCambio valore della variabile reattivo in gancio in Meteor

Template.templateName.onCreated(function() { 
    this.variableName = new ReactiveVar; 
    this.variableName.set(true); 
}); 

in templateName ho un autoform. Devo impostare la variabile reattiva variableName a false quando viene inviato il numero autoform.

ho cercato

AutoForm.hooks({ 
    myForm: { 
    onSuccess: function(operation, result) { 
     this.variableName.set(false); 
    }, 
    } 
}); 

ma non funziona in quanto this. non si riferisce al modello templateName come fa in aiutanti ed eventi. Avrebbe funzionato se avessi usato le sessioni, poiché queste non sono limitate/mirate a modelli specifici.

Cosa posso fare per modificare la variabile reattiva in un gancio di autoformata?

Ho anche provato

AutoForm.hooks({ 
    myForm: { 
    onSuccess: function(operation, result) { 
     this.template.variableName.set(false); 
     this.template.parent.variableName.set(false); 
     this.template.parent().variableName.set(false); 
     this.template.parentData.variableName.set(false); 
     this.template.parentData().variableName.set(false); 
     this.template.parentView.variableName.set(false); 
     this.template.parentView().variableName.set(false); 
    }, 
    } 
}); 

Quando si utilizza console.log(this.template) lo fa stampare un oggetto. Se uso console.log(this.template.data) ricevo

Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…} 

Io uso la variabile reattivo variableName per determinare se sia mostrare la forma modificabile o la bella presentazione dei dati per l'utente. Forse c'è un altro modo migliore per farlo.

+0

Ho affrontato lo stesso scenario ma l'ho risolto usando 'Session' e' autorun'. E "onDestroyed" di quel template, renderò la 'Session' come' null' ... Aspettando di conoscere qualsiasi altro modo migliore per risolvere questo ... Thumps Up! – iamhimadri

+0

@Jamgreen, i codici 'Template.templateName.onCreated' e' AutoForm.hooks' si trovano sullo stesso file? –

+2

Hai provato a usare 'Template.instance(). VariableName' nel tuo hook? – SylvainB

risposta

1

Modifica di onCreated:

Template.templateName.onCreated(function() { 
    this.variableName = new ReactiveVar(false); 
}); 

si può decidere di aggiungere una funzione di supporto per afferrare l'istanza Template:

Template.templateName.helpers({ 
    getVariableName: function() { 
     return Template.instance().variableName.get(); 
    } 
}); 

e quindi si potrebbe chiamare all'interno del logica modulo

AutoForm.hooks({ 
    myForm: { 
    onSuccess: function(operation, result) { 
     Template.getVariableName.set(false); 
    }, 
    } 
}); 

Il MeteorChef ha un grande articolo sulle variabili reattive/dizionari Reactive Variables.

0

Se la gente inciampare su questo per una soluzione, basata this thread io come in grado di accedere ai genitori reattiva Dict/variabile reattivo dopo l'installazione aldeed:template-extension in questo modo

AutoForm.hooks({                               
    postFormId: { 
    //onSuccess hook of my post form 
    onSuccess: function(formType, result) { 
     this.template.parent().reactiveDictVariable.set('imgId', 'newValueSetOnSuccess'); 
     //Or reactive var, and depending on your hierarchy 
     //this.template.parent().parent().yourReactiveVar.set(undefined); 
    } 
    } 
}); 

Qui è HTML e JS per riferimento.

<template name="postsTemplate"> 
    {{#autoForm schema=postFormSchema id="postFormId" type="method" meteormethod="addPost"}} 
    <!-- other autoform stuff --> 
    This is reactive variable used in template -- {{imgId}} 
    {{/autoForm}} 
</template> 


Template.postsTemplate.created = function() { 
    //Using reactive-dict package here                         
    this.reactiveDictVariable = new ReactiveDict(); 
    this.reactiveDictVariable.set('imgId', undefined); 
}; 

Template.posts.events(
    "change .some-class": function(event, template) { 
    //other stuff 
    template.postImgState.set('imgId', 'something'); 
} 
Problemi correlati