整个页面在渲染之前等待用户确认

时间:2016-08-31 14:53:10

标签: javascript jquery knockout.js confirm

我要做的很简单:用户打开页面,获得一个要求他选择A或B的确认模式,然后整个页面将呈现取决于选择。我的问题是页面保持向前呈现而不等待用户确认。我怎么能这样做?

我的代码工作流程 utils.js

callForModal()//Code that available for all pages

用于淘汰赛的viewModels.js

customBindingHandler.svg = {
    init: function (element, valueAccessor) {
        // Render depending on callForModal choices here
        // call render function inside callForModal() callback does not work here
    }
}

viewModels(){
    // Promised ajax with some data loading before stuff happen
    // Put the callForModal() does not also work here
}

1 个答案:

答案 0 :(得分:1)

使用templates。您希望随时渲染三个页面中的一个。每个页面都是一个模板。有一个可观察的,你设置为所需模板的ID。

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<template id="modal-choice">
  Choose:
  <button data-bind="click:$data.changeTemplate('A')">A</button>or
  <button data-bind="click:$data.changeTemplate('B')">B</button>
</template>
<template id="A">
  You chose template A!
</template>
<template id="B">
  B was your choice!
</template>
<div data-bind="template: t"></div>
/*************************************************************
ARDUINO BREAKER READ/SPI PRE-PROC/TRANSMIT CASES
****************************************************************/

/***************************************************************
 Global Variables
***************************************************************/

byte command = 0; //command from PI
byte bytes[4];    //

int sensorVoltage, sensorCurrent; //eventual live reading vars
float Voltage, Current, RealCurrent, RealVoltage, Power;

/***************************************************************
 Set Up
  -designate arudino as slave
  -turn on interrupts
***************************************************************/

void setup (void)
{
  //debugging with serial monitor
  Serial.begin(9600);

  // Set up arduino as slave
  pinMode(MOSI, INPUT);
  pinMode(SCK, INPUT);
  pinMode(SS, INPUT);   
  pinMode(MISO, OUTPUT);

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);

}  // end of setup

/*************************************************************
 Interrupt Service Routine 
 ************************************************************/

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  delay(500); //for errors

  // Create union of shared memory space
  union 
  {
    float f_var;
    unsigned char bytes[4];
  } u;

  // Overwrite bytes of union with float variable
  u.f_var = RealVoltage;

  // Assign bytes to input array
  memcpy(bytes, u.bytes, 4);

  byte c = SPDR;
  command = c; 

  switch (command)
  {
  // null command zeroes register
  case 0:

    SPDR = 0;
    break;

  // case a - d reserved for voltage
  case 'a':
    SPDR = bytes[3];  
    break;

  // incoming byte, return byte result
  case 'b':

    SPDR = bytes[2];  
    break;

  // incoming byte, return byte result    
  case 'c':

    SPDR =  bytes[1];  
    break;


  // incoming byte, return byte result    
  case 'd':

    SPDR = bytes[0];  
    break;

 /**  // case e -h reserved for current
  case 'e':

    SPDR = amps.b[0];  
    break;

  // incoming byte, return byte result
  case 'f':

    SPDR = amps.b[1];  
    break;

  // incoming byte, return byte result    
  case 'g':

    SPDR = amps.b[2];  
    break;

  // incoming byte, return byte result    
  case 'h':

    SPDR = amps.b[3];  
    break;

   // case i - l reserved for wattage
  case 'i':

    SPDR = watts.b[0];  
    break;

  // incoming byte, return byte result
  case 'j':

    SPDR = watts.b[1];  
    break;

  // incoming byte, return byte result    
  case 'k':

    SPDR = watts.b[2];  
    break;

  // incoming byte, return byte result    
  case 'l':

    SPDR = watts.b[3];  
    break;**/

  } // end of switch

}  // end of interrupt service routine (ISR) SPI_STC_vect

/***************************************************************  
 Loop until slave is enabled by Pi.
****************************************************************/
void loop (void)
{
/*************************************************************
Read and Calculate
****************************************************************/

  /**
  sensorVoltage = analogRead(A2);
  sensorCurrent = analogRead(A3);
  Voltage = sensorVoltage*(5.0/1023.0);
  Current = sensorCurrent*(5.0/1023.0);
  RealCurrent = Current/0.204545;
  RealVoltage = (Voltage/0.022005);
  Power = RealVoltage*RealCurrent;
**/
  RealVoltage = 1.234;
/*************************************************************
Loop Check for SS activation
****************************************************************/

  // if SPI not active, clear current command, else preproc floats and pass to SPI
  if (digitalRead (SS) == HIGH){
    command = 0;
  }
/*************************************************************
Debug with serial monitor
****************************************************************/
/*
  Serial.print("Byte 3: ");
  Serial.println(bytes[3],BIN);
  delay(500);
  Serial.print("Byte 2: ");
  Serial.println(bytes[2],BIN);
  delay(500);
  Serial.print("Byte 1: ");
  Serial.println(bytes[1],BIN);
  delay(500);
  Serial.print("Byte 0: ");
  Serial.println(bytes[0],BIN);
  delay(1000);
  Serial.println();*/
}