abritopach / ionic-surveyjs

Sample project that shows how to integrate SurveyJS in Ionic APP.
MIT License
28 stars 18 forks source link

Integrating NavController into onComplete #1

Closed wollerman closed 6 years ago

wollerman commented 6 years ago

Thanks for the great example repo! I'm still learning the ropes of surveyJS and Ionic, so this might be a silly question and hopefully you don't mind:

I'm trying to integrate the navcontroller into my survey component:

import {Component, Input} from '@angular/core';
import * as Survey from 'survey-angular';
import {NavController} from "ionic-angular";

/**
 * Generated class for the SurveyComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'survey',
  templateUrl: 'survey.html'
})
export class SurveyComponent {

  surveyJSON: any;

  @Input()
  set json(surveyJSON) {
    Survey.Survey.cssType = "bootstrap";
    Survey.defaultBootstrapCss.navigationButton = "btn btn-green";
    this.surveyJSON = surveyJSON;
    let surveyModel = new Survey.ReactSurveyModel(surveyJSON);
    // Progress Bar.
    surveyModel.showProgressBar = 'bottom';
    // surveyModel.onComplete.add(this.sendDataToServer);
    surveyModel.onComplete.add(this.doPop);
    Survey.SurveyNG.render('surveyElement', {model: surveyModel});
  }

  constructor(public navCtrl: NavController) {
    console.log('survey component loaded');
    console.log(this.navCtrl);
  }

  doPop() {
    console.log(this.navCtrl);
    this.navCtrl.pop();
  }

}

When the page gets rendered I see the navcontroller in the console as expected from the constructor. However, once the survey is complete this.navCtrl is undefined inside doPop():

TypeError: Cannot read property 'pop' of undefined
    at Array.webpackJsonp.287.SurveyComponent.doPop (http://localhost:8100/build/main.js:1053:22)
    at Event.fire (http://localhost:8100/build/vendor.js:122947:47)
    at ReactSurveyModel.SurveyModel.doComplete (http://localhost:8100/build/vendor.js:126150:25)
    at ReactSurveyModel.SurveyModel.completeLastPage (http://localhost:8100/build/vendor.js:126102:14)
    at SurveyNavigation.handleCompleteClick (http://localhost:8100/build/vendor.js:131314:21)
    at HTMLInputElement.eventProxy (http://localhost:8100/build/vendor.js:131937:39)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15040)
    at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4238:33)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:14961)
    at r.runTask (http://localhost:8100/build/polyfills.js:3:10214)

It seems the issue is that the this context gets lost once the doPop() is added to onComplete. Any suggestions on how to integrate the navcontroller into my surveyModel.onComplete call?

Thanks!

wollerman commented 6 years ago

I also created an issue with the same question on the surveyjs project where I was given an answer. So closing this.