2011-12-09 6 views
0

私はSilverlightからKnockout.jsに来ています。ビューモデルでのカスタムオブジェクトの使用ノックアウト

public class MyViewModel : ViewModel 
{ 
    private MyCustomClass custom = null; 
    public MyCustomClass Custom 
    { 
    get { return custom; } 
    set { custom = value; 
     NotifyPropertyChanged("MyCustomClass"); 
    } 
    } 
} 

<TextBox Text="{Binding Path=Custom.PropertyName, Mode=TwoWay}" /> 

しかし、私はKnockout.js中のものと同じ種類の操作を行うするかどうかはわかりません。私はSilverlightでのviewmodelsを作成したとき、私はしばしば、このようなものを持っているでしょう。現在、私が持っている:

<input type="text" data-bind="value:propertyName" /> 

var viewModel = { 
    custom: { 
    propertyName:"" 
    } 
} 

答えて

1

あなたは確かにあなたがそこに持っているものを行うことができますが、あなたはすでにKO 1.3に結合テンプレートまたはwithを使用してcustomにコンテキストを変更していない限り、value: custom.propertyNameにバインドしたいと思います。

var Person = function(first, last) { 
    this.first = ko.observable(first); 
    this.last = ko.observable(last); 
}; 

var viewModel = { 
    people: ko.observableArray([ 
    new Person("Bob", "Smith"), 
    new Person("Ted", "Jones") 
    ]) 
}; 

これを行うための一般的な方法は以下のように、カスタムオブジェクトのコンストラクタ関数を作成することです

関連する問題