我的視圖模型有幾個可觀察的屬性,我使用它們來存儲下拉列表中的值,它基本上就像這裏解釋的購物車示例一樣。ko.obervable正在更新兩次
knockoutjs 'Cart editor' example questions
,這裏是我的html 1)選擇boardtest 2)每個區選擇分數
<select data-bind="options :$parent.boardTestData,optionsText:'name', value:boardTestSelected, optionsCaption:'Select Test' "> </select>
<div data-bind="visible : boardTestSelected, foreach : boardTestSelected() ? boardTestSelected().testParts : null">
<div>
<span data-bind="text: boardTestPartName"></span>
<select data-bind="options: testPartValues, optionsText:'BoardTestPartValue',
value: $parent.testPartValueSelected> </select>
</div>
</div>
我的視圖模型具有
$ parent.boardTestData是obervablearray()與testParts [] 和testParts包含另一個數組testPartValues []
class @BoardTestData
constructor :(data) ->
@id = data.Id
@name = data.Name
@testParts = []
@testParts.push new TestParts data.Name,d for d in data.TestParts
class @TestParts
constructor:(partData) ->
@testPartValues = []
class @TestPartValues
constructor:(partValues) ->
@BoardTestPartValueId= partValues.Id
@BoardTestPartValue = partValues.Value
@boardTestSelected = ko.observable()
@testPartSelected = ko.observable()
@testPartValueSelected = ko.observable()
我訂閱了testPartValueSelected,並且它第一次執行了兩次正確的值,然後是witj空值,所以我認爲有些東西把@testPartValueSelected設置爲null或undefined。
- 它在某種程度上正常工作時,有在foreach的嵌套下拉列表只是一個單一的項目
,雖然我這樣做,我有是有可能推選定值的其他問題從下拉列表或數組或obersvable數組()?
搭建JS搗鼓這個問題:http://jsfiddle.net/paisley/W82yE/11/
你能否提供一個合適的javascript例子?我不知道代碼是什麼,但它看起來不像javascript:( – soniiic 2012-02-21 11:00:25
它實際上是咖啡腳本。 – Neel 2012-02-21 14:59:42