Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm marking Derick's answer as accepted as it's cleaner than what I have, but it's not a solution I can use in my case as I have 50+ screens to deal with that I can't break into nice models - I have been given their content and simply have to replicate them.</p> <p>Here's the hacky model code that I came up with which handles screen switching logic. I'm sure I'll end up refactoring it a lot more as I continue working on it.</p> <pre><code>var Wizard = Backbone.Model.extend({ initialize: function(options) { this.set({ pathTaken: [0], // instantiate the screen definitions as screenResponses screenResponses: _(this.screens).map(function(s){ return new s; }) }); }, currentScreenID: function() { return _(this.get('pathTaken')).last(); }, currentScreen: function() { return this.screens[this.currentScreenID()]; }, isFirstScreen: function(screen) { screen = screen || this.currentScreen(); return (_(this.screens).first() === screen); }, // This function should be overridden by wizards that have // multiple possible "finish" screens (depending on path taken) isLastScreen: function(screen) { screen = screen || this.currentScreen(); return (_(this.screens).last() === screen); }, // This function should be overridden by non-trivial wizards // for complex path handling logic nextScreenID: function(currentScreenID, currentScreen) { // default behavior is to return the next screen ID in the list return currentScreenID + 1; }, nextScreen: function() { // return immediately if on final screen if (this.isLastScreen()) return; // otherwise get next screen id from nextScreenID function nsid = this.nextScreenID(this.currentScreenID(), this.currentScreen()); if (nsid) { this.get('pathTaken').push(nsid); return nsid; } }, prevScreen: function() { // return immediately if on first screen if (this.isFirstScreen()) return; // otherwise return the previous screen in the list prevScreenID = _(_(this.get('pathTaken')).pop()).last(); return this.screens[prevScreenID]; } }); var ChocolateWizard = Wizard.extend({ initialize: function(options) { Wizard.prototype.initialize.call(this); // super() this.set({ wizardType: 'Chocolate', }); }, nextScreenID: function(csid, cs) { var resp = this.screenResponses(csid); this.nextScreenController.setState(csid.toString()); // have to manually change states if (resp.nextScreenID) // if screen defines a custom nextScreenID method, use it return resp.nextScreenID(resp, this.get('screenResponses')); else // otherwise return next screen number by default return csid + 1; }, // convenience function screenResponses: function(i) { return this.get('screenResponses')[i]; }, screens: [ // 0 Backbone.Model.extend({ title : "Chocolate quiz", schema: { name: 'Text', likesChocolate: { title: 'Do you like chocolate?', type: 'Radio', options: ['Yes', 'No'] } }, nextScreenID: function(thisResp, allResp) { if (thisResp.get('likesChocolate') === 'Yes') return 1; else return 2; } }), // 1 Backbone.Model.extend({ title : "I'm glad you like chocolate!", schema: { chocolateTypePreference: { title: 'Which type do you prefer?', type: 'Radio', options: [ 'Milk chocolate', 'Dark chocolate' ] } }, nextScreenID: function(thisResp, allResp) { return 3; // skip screen 2 } }), // 2 Backbone.Model.extend({ title : "So you don't like chocolate.", schema: { otherSweet: { title: 'What type of sweet do you prefer then?', type: 'Text' } } }), // 3 Backbone.Model.extend({ title : "Finished - thanks for taking the quiz!" } ] }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload