Coffeescript 如何在coffescript中对敲除计算属性使用读写
我将非常感谢任何帮助。我无法在CoffeeScript中正确写入计算属性的读写操作Coffeescript 如何在coffescript中对敲除计算属性使用读写,coffeescript,knockout-2.0,Coffeescript,Knockout 2.0,我将非常感谢任何帮助。我无法在CoffeeScript中正确写入计算属性的读写操作 @allChecked = ko.computed => { read: ()-> console.log 'allChecked->read()' firstUnchecked = ko.utils.arrayFirst @contactGroups(), (item) -> item.IsSelected() == fal
@allChecked = ko.computed => {
read: ()->
console.log 'allChecked->read()'
firstUnchecked = ko.utils.arrayFirst @contactGroups(), (item) ->
item.IsSelected() == false
firstUnchecked == null
write: (value)->
console.log 'allChecked->write()', value
g.IsSelected(value) for g in @contactGroups()
}
我只是盲目猜测,因为我无法访问您的其余代码 弗斯特
ko.computed
采用读取函数,或采用具有读取
和写入
函数的对象。它不接受返回具有read
/write
属性的对象的函数
示例
右:ko.computed->5
右:ko.computed{read:->5}
错误:ko.computed->{read:->5}
第二
@
严格地说是this
,这意味着根据调用函数的方式(f()
,f.apply()
,new f()
),它可以有不同的值。如果要指定此
的值,可以在创建ko.computed
时指定所有者
computed = ko.computed {
read: -> @getValue()
owner: @
}
示例
好
class Thing
constructor: (@number) ->
self = @
ko.computed -> self.number
class Thing
constructor: (@number) ->
ko.computed -> @number # means this.number
ko-way
class Thing
constructor: (@number) ->
ko.computed {
read: -> @number
owner: @
}
坏的
class Thing
constructor: (@number) ->
self = @
ko.computed -> self.number
class Thing
constructor: (@number) ->
ko.computed -> @number # means this.number
令人困惑(=>)
最后
把它们放在一起
示例