javascript - Knockout.js 问题 : "h.apply is not a function. (In ' h. apply(e,r )', ' h.apply' 未定义)"

标签 javascript knockout.js data-binding prototype-programming

所以我尝试通过数据绑定(bind)调用我的 viewModel 原型(prototype)上的方法。我通过“单击”将两个不同的元素数据绑定(bind)到同一方法。当我单击第一个按钮(“新游戏”按钮)时,它会显示我想要显示的内容。当我点击 <td>与数据绑定(bind)关联,它会抛出错误“类型错误:h.apply 不是函数。(在 'h.apply(e,r)' 中,'h.apply' 未定义)。我做错了什么在这里?

我所说的方法是 viewModel 原型(prototype)上的 Messages 方法。

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 预处理器)

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

最佳答案

点击绑定(bind)需要调用一个函数。看来您正在调用该函数,并绑定(bind)到调用该函数的结果。删除函数调用。

更改此:

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

对此:

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

关于javascript - Knockout.js 问题 : "h.apply is not a function. (In ' h. apply(e,r )', ' h.apply' 未定义)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927346/

相关文章:

javascript - CKEDITOR 小部件拖放到所需元素中

events - 如何在 Angularjs 中使用 $event 在事件点击时更新 ng-model

winforms - Windows 窗体单选按钮数据绑定(bind)

c# - DropDownList 中的 ASP.NET Bind() - 字符串值与 Null

javascript - 使用ajax向服务器发送数据的问题

javascript - 从 jquery 自动完成中获取不同的名称

javascript - 如何使用数据绑定(bind) Knockout.js 枚举 javascript 对象的属性

javascript - Sequelize 查询符合 $LIKE 的要求,但收到 ER_PARSE_ERROR

javascript - Jquery Cycle2 渐进式加载显示 &lt;script&gt; 标签

javascript - 是否可以将 Knockout.JS 变量传递到 onclick 函数而不进行绑定(bind)?