2013-03-04 56 views
0

存在HTML數據綁定設置器的問題。我希望它設置爲模型(exerciseCategories)間隔值。 如果我綁定到模型的間隔,它是正確的值,但不可觀察。 如果我將它綁定到$ parent.intervals,它是來自viewModel的默認值(1),但它是可觀察的。 我想都:)。我究竟做錯了什麼? 像這樣的工作,但顯示[目標對象]:KnockoutJS數據綁定設置器

<td data-bind='with: exercise'> 
    <input data-bind='value: $parent.intervals(intervals)' /> 
</td> 

What I've got is - HTML  
      ... 
      <td> 
       <select data-bind='options: exerciseCategories , optionsText: "category", optionsCaption: "Izberite...", value: exerciseType'></select> 
      </td> 
      <td data-bind="with: exerciseType"> 
       <select data-bind='options: exercises, optionsText: "title", optionsCaption: "Izberite...", value: $parent.exercise'></select> 
      </td> 
      <td data-bind='with: exercise'> 
        <input data-bind='value: $parent.intervals' /> 
      </td> 
      ... 
JavaScript 
    var exerciseCategories = [ 
    { 
     exercises: [{ 
      title: 'Aerobic exercise #1', 
      intervals: 2 
     }], 
     category: 'Aerobics' 
    }]; 

     var Exercise = function() { 
       var self = this; 

       self.exerciseType = ko.observable(); 
       self.exercise = ko.observable(); 
       self.intervals = ko.observable(1); 
      }; 
+0

你是如何獲取數據到您的視圖模型? – 2013-03-04 10:51:59

+0

你將不得不提供更多的代碼。我缺少與綁定的for:綁定和視圖模型的表標記。我認爲你在混合觀看模型。 – nickvane 2013-03-04 10:59:12

+0

這只是摘錄。整體幾乎相同http://jsfiddle.net/rniemeyer/adNuR/ – kayz1 2013-03-04 11:57:39

回答

0

做$ parent.intervals(間隔)要調用的時間間隔觀察到的函數傳遞的時間間隔作爲參數,顯然你會收到KO。作爲結果的可觀察對象。

我得到了你的摘錄工作。看看這個http://jsfiddle.net/MhHc4/

HTML

Categories: 
<select data-bind='options: exerciseCategories , optionsText: "category", optionsCaption: "Izberite...", value: selectedCategory'></select> 
<p>selectedCategory() debug: <pre data-bind="text: selectedCategory() ? ko.toJSON(selectedCategory().exercises, null, 2) : ''"></pre> 
</p>Exercises: 
<select data-bind='options: selectedCategory() ? selectedCategory().exercises : [], optionsText: "title", value: selectedExercise'></select> 
<p>selectedExercise() debug: <pre data-bind="text: selectedExercise() ? ko.toJSON(selectedExercise(), null, 2) : 'x'"></pre> 
</p> 
<input type="text" data-bind="attr : { value : selectedExercise() ? selectedExercise().intervals : 0 }"/> 

的Javascript

var exerciseCategories = [{ 
    exercises: [{ 
     title: 'Aerobic exercise #1', 
     intervals: 2 
    }], 
    category: 'Aerobics' 
}]; 

var ExerciseViewModel = function() { 
    var self = this; 

    self.exerciseCategories = ko.observable(exerciseCategories); 
    self.selectedCategory = ko.observable(); 
    self.selectedExercise = ko.observable(); 
    self.intervals = ko.observable(1); 
}; 
ko.applyBindings(new ExerciseViewModel()); 

HTH

+0

這不是我想要的。如果您最終將放置爲默認值。 – kayz1 2013-03-05 06:27:55