2013-07-31 49 views
3

我开始使用淘汰赛,并且点击事件没有解决问题。MVC4淘汰赛数据绑定点击事件没有触发

上述未烧成GetWaiters功能。不知道我在做什么错误或缺失。

TIA

我有以下的html:

<h2>Create</h2> 
<table> 
    <thead> 
     <tr> 
      <th>WaiterId</th> 
      <th>RestId</th> 
      <th>Name</th> 
     </tr> 
    </thead> 
    <tbody data-bind="foreach: Waiters"> 
     <tr> 
      <td data-bind="text: waiter_id"></td> 
      <td data-bind="text: rest_id"></td> 
      <td data-bind="text: name"></td> 
     </tr> 
    </tbody> 
</table> 
<br /> 
@Scripts.Render("~/bundles/myBundle") 
<input type="button" id="btnGetWaiters" value="Get Waiters" data-bind="click: GetWaiters" /> 

And following in my js file: 

var WaiterViewModel = function() { 
    //Make the self as 'this' reference 
    var self = this; 
    //Declare observable which will be bind with UI 
    self.waiter_id = ko.observable("0"); 
    self.rest_id = ko.observable("0"); 
    self.name = ko.observable(""); 

    //The Object which stored data entered in the observables 
    var WaiterData = { 
     waiter_id: self.waiter_id, 
     rest_id: self.rest_id, 
     name: self.name 
    }; 

    //Declare an ObservableArray for Storing the JSON Response 
    self.Waiters = ko.observableArray([]); 

    GetWaiters(); //Call the Function which gets all records using ajax call 

    //Function to Read All Employees 
    function GetWaiters() { 
     alert("fetching"); 
     //Ajax Call Get All Employee Records 
     $.ajax({ 
      type: "GET", 
      url: "/api/WaiterAPI", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (data) { 
       self.Waiters(data); //Put the response in ObservableArray 
      }, 
      error: function (error) { 
       alert(error.status + "<--and--> " + error.statusText); 
      } 
     }); 
     //Ends Here 
    } 
}; 
ko.applyBindings(new WaiterViewModel()); 

回答

8

当你试图绑定点击事件绑定方法应该是视图模型,但在您的实现GetWaiters()的方法声明为private方法。定义这样的方法,然后再试一次:

self.GetWaiters = function() { 
    // your code 
}; 
+0

非常感谢vadim。 –

+0

进行了更改并尝试调用它:self.GetWaiters()。我收到以下错误:TypeError:self.GetWaiters不是函数 –

+0

对不起我的坏。在函数下面移动了调用self.GetWaiters(),它工作。 –

2

你的函数很好,但是你声明的方式不正确。

首先尝试检查您的型号console.log(new WaiterViewModel())。你会发现它没有任何名称为GetWaiters()的功能。从@vadim回忆回答GetWaiters() is declared as private method

所有你需要做的就是将GetWaiters()与你的viewmodel关联起来。比如像你这样有waiter_id什么,所以其声明是这样的:

self.GetWaiters = function() { //your code goes here } 

调用它使用self.GetWaiters()

0

小提示:

我曾与事件点击结合不类似的问题射击。问题是拼写错误函数名称在我的情况。问题是敲除在这种情况下是非常沉默的,因为选择了立即执行的绑定,并在控制台中获得适当的错误。