简体   繁体   English

Knockout.js问题:“ h.apply不是函数。 (在“ h.apply(e,r)”中,“ h.apply”未定义)”

[英]knockout.js issues: “h.apply is not a function. (In 'h.apply(e,r)', 'h.apply' is undefined)”

So I am trying to call a method on my viewModel prototype through data binding. 所以我试图通过数据绑定在我的viewModel原型上调用一个方法。 I data-bind two different elements to the same method, both via "click". 我通过“点击”将两个不同的元素数据绑定到相同的方法。 When I click the first button (New Game button), it displays what I want it to display. 当我单击第一个按钮(“新游戏”按钮)时,它将显示我想要显示的内容。 When I click the <td> associated with the data-bind, it throws the error "Type Error: h.apply is not a function. (In 'h.apply(e,r)', 'h.apply' is undefined). What am I doing wrong here? 当我单击与数据绑定关联的<td> ,它将引发错误“类型错误:h.apply不是函数。(在'h.apply(e,r)'中,'h.apply'未定义)。我在这里做错了什么?

The method I'm talking about is the Messages method on the viewModel prototype. 我正在谈论的方法是viewModel原型上的Messages方法。

JAVASCRIPT JAVASCRIPT

var message = (function(){
  function Message(){
   this.main = ko.observable(true);
   this.welcome = "Welcome to Tic-Tac-Toe! This is a 2 player game. Click New Game to play!"
   this.turn = ", its your turn."
   this.win = ", you won!"
   this.draw = "It's a draw..."
  }
  return Message;
})()

var players = (function(){
  function Players(){
    this.player1 = ko.observable(true);
    this.player2 = ko.observable(false);
  }
  return Players;
})()

var aBox = (function(){
  function ABox(){
    this.symbol = ko.observable(" ")
  }

  return ABox;
})()

var viewModel = (function(){
  function ViewModel(){
    this.GameMessage = new message();
    this.thePlayers = new players();
    this.r1c1 = new aBox();
    this.r1c2 = new aBox();
    this.r1c3 = new aBox();
    this.r2c1 = new aBox();
    this.r2c2 = new aBox();
    this.r2c3 = new aBox();
    this.r3c1 = new aBox();
    this.r3c2 = new aBox();
    this.r3c3 = new aBox();

  }

/**************************************** 
 ************* Messages *****************
 ****************************************/ 

  ViewModel.prototype.StartMessage = function(){

     this.GameMessage.main(false)
  }

  ViewModel.prototype.Messages = function(){

    if(this.GameMessage.main()){
      return this.GameMessage.welcome;
    }
    else if(this.thePlayers.player1()){
      this.thePlayers.player1(false);
      this.thePlayers.player2(true);
      return "Player 1"+this.GameMessage.turn;

    }
    else if(this.thePlayers.player2())
      this.thePlayers.player1(true);
      this.thePlayers.player2(false);
      return "Player 2"+this.GameMessage.turn;
  }






/**************************************** 
 ************* GamePlay *****************
 ****************************************/ 

/**************************************** 
 ************* If needed ****************
 ****************************************/ 






  return ViewModel;
})()

ko.applyBindings(new viewModel())

HTML(Jade Preprocessor) HTML(玉预处理器)

p#title.col-xs-12.bg-primary.text-center
  | Tic - Tac - Toe!
div.col-xs-3.bg-info
  div.bg-primary.controls
    span
      button.btn.btn-default(data-bind="click:StartMessage.bind($root)")
        | New Game
      p#message.lead(data-bind="text:Messages.bind($root)()")
table.bg-success(style="table-layout:fixed;")
  tr#row1
    td(data-bind="click:Messages.bind($root)()")
    td &nbsp
    td &nbsp
  tr#row2
    td &nbsp 
    td &nbsp
    td &nbsp
  tr#row3
    td &nbsp 
    td &nbsp
    td &nbsp

The click binding requires a function to call. 点击绑定需要一个函数来调用。 It appears you are invoking that function instead, binding to the result of calling the function. 看来您是在调用该函数,而是绑定到调用该函数的结果。 Remove the function call. 删除函数调用。

Change this: 更改此:

td(data-bind="click:Messages.bind($root)()")

to this: 对此:

td(data-bind="click:Messages.bind($root)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM