淘汰赛。js - xxxx不是一个函数。

时间:2021-11-20 20:34:08

I am trying to incorporate Knockout.js into a WebApplication.

我正在尝试合并淘汰赛。js WebApplication。

The tutorial I am basing much of my code on is here.

我的大部分代码都基于本教程。

Basically - I have a list of items - I want to be able to click an item and have the corresponding data appear in a div at the bottom of the page. Eventually I'll use jquery.UI Dialog plugin to turn this div into a popup, but for now, I'm just trying to get the selectedItem to work.

基本上——我有一个项目列表——我希望能够单击一个项目,并将相应的数据显示在页面底部的div中。最终我将使用jquery。UI对话框插件将这个div变成一个弹出窗口,但是现在,我只是想让selectedItem工作。

My (simplified) code is here: http://jsfiddle.net/fZXAX/1/

我的(简化的)代码在这里:http://jsfiddle.net/fZXAX/1/

I just get the error: actionListViewModel.selectedActionId is not a function.

我得到的错误是:actionListViewModel。selectedActionId不是函数。

I don't see the difference between this and the tutorial which uses selectedMailId in an identical way. The only difference between my code and the example is that I am not using literal notation.

我没有看到这个和使用selectedMailId的教程的区别。我的代码和示例之间的唯一区别是,我没有使用文字符号。

Can anyone see where I am going wrong? Thanks in advance.

有人能看出我哪里做错了吗?提前谢谢。

1 个解决方案

#1


7  

Your error is here:

你的错误是:

click: function() {actionListViewModel.selectedActionId(id)}

actionListViewModel is a constructor function, but you're acting as if it's an object.

actionListViewModel是一个构造函数,但是你的行为好像它是一个对象。

See this forked jsFiddle. This line, which defines a constructor function

看到这个分叉的jsFiddle。这一行定义了构造函数

function actionListViewModel () {

was changed to be an instance of a new object, created by calling an anonymous constructor function.

被更改为一个新对象的实例,通过调用匿名构造函数创建。

var actionListViewModel = new function () {

and this line, where you were creating an instance of your previously defined function

这一行,你在创建一个你之前定义的函数的实例。

ko.applyBindings(new actionListViewModel());

was changed to just pass in the instance that we setup earlier

被更改为只传入我们前面设置的实例

ko.applyBindings(actionListViewModel);

Alternatively you could simply define a variable viewModel and set it = new actionListViewModel(); and then update your click literal to point to viewModel instead of actionListViewModel. You can see that approach here

或者,您可以简单地定义一个变量viewModel,并设置它= new actionListViewModel();然后更新单击文字指向viewModel而不是actionListViewModel。你可以在这里看到这种方法

#1


7  

Your error is here:

你的错误是:

click: function() {actionListViewModel.selectedActionId(id)}

actionListViewModel is a constructor function, but you're acting as if it's an object.

actionListViewModel是一个构造函数,但是你的行为好像它是一个对象。

See this forked jsFiddle. This line, which defines a constructor function

看到这个分叉的jsFiddle。这一行定义了构造函数

function actionListViewModel () {

was changed to be an instance of a new object, created by calling an anonymous constructor function.

被更改为一个新对象的实例,通过调用匿名构造函数创建。

var actionListViewModel = new function () {

and this line, where you were creating an instance of your previously defined function

这一行,你在创建一个你之前定义的函数的实例。

ko.applyBindings(new actionListViewModel());

was changed to just pass in the instance that we setup earlier

被更改为只传入我们前面设置的实例

ko.applyBindings(actionListViewModel);

Alternatively you could simply define a variable viewModel and set it = new actionListViewModel(); and then update your click literal to point to viewModel instead of actionListViewModel. You can see that approach here

或者,您可以简单地定义一个变量viewModel,并设置它= new actionListViewModel();然后更新单击文字指向viewModel而不是actionListViewModel。你可以在这里看到这种方法