gpt4 book ai didi

javascript - Knockout 和 coffeescript - uncouched 类型错误

转载 作者:行者123 更新时间:2023-11-30 06:38:10 25 4
gpt4 key购买 nike

我想使用 knockout 和 CoffeeScript 维护简单的应用程序。我想添加 Json 对象数组,例如:

[
{
Name = "",
Subscribe = "",
ScrollOfDish = [0]
},
{
Name = "",
Subscribe = "",
ScrollOfDish = [0]
}
]

这是我的代码:

class Complex
constructor: ()->
@ComplexItems = ko.observableArray()
@getAllComplex()
self = this

getAllComplex:()->
$.ajax
type: "POST"
url: "/Complex/GetAllComplex"
data: {}
dataType:"json"
contentType:"json"
success:(res)->
self.ComplexItems res // here is the problem


$(document).ready ()->
window.model = new Complex()
ko.applyBindings(model)

但我遇到了未修饰类型错误 ComplexItems is not a function 的问题。当然,我尝试反向 self.ComplexItemsmodel.ComplexItems - 它不起作用 - 但我想了解为什么我的示例不起作用。

最佳答案

你的 self = this 是在错误的地方.. 现在它使“self”指向构造函数(而不是类的任何实例..)。应该是:

class Complex
constructor: ()->
@ComplexItems = ko.observableArray()
@getAllComplex()

getAllComplex:()->
self = this
$.ajax
type: "POST"
url: "/Complex/GetAllComplex"
data: {}
dataType:"json"
contentType:"json"
success:(res)->
self.ComplexItems res


$(document).ready ()->
window.model = new Complex()
ko.applyBindings(model)

关于javascript - Knockout 和 coffeescript - uncouched 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13272021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com