knockoutjs嵌套的可观察对象和coffeescript类

时间:2012-05-06 20:24:01

标签: nested coffeescript knockout.js observable

我有一个播放器类和viewmodel

class Player
    constructor: ->
        @boat = ko.observable null

class Boat
    constructor: (@id) ->

class ViewModel
    player: ko.observable 

in html

<div data-bind="if: player">
        <div data-bind="template: {name: 't_me', data: player}"></div>
</div>
<script type="text/html" id="t_me">
    <span>player boat
        <span data-bind="if: boat()">            
            <b data-bind="text: boat().id"></b>
        </span>        
    </span>
</script>

现在在脚本中我尝试将船设置为plyer

vm = new ViewMode()
ko.applyBindings vm
vm.player new Player()
vm.player.boat new Boat(1)

我无法让View对此进行任何改变做出反应?

1 个答案:

答案 0 :(得分:0)

我在代码中看到的唯一问题是vm.player可观察对象缺少parens。 (假设vm = new ViewMode()是拼写错误)

我相信最后一行应该是:vm.player().boat new Boat(1)