私はviewmodelのオブジェクトに日付をバインドする際に問題があります。私はサーバーから取得している日付があります。KnockoutJSの日付バインディングの問題、jQueryのdatepickerを使用して
var viewModel = {
profile : ko.mapping.fromJS(initialData),
プロパティをテキストボックスにバインドしています。
<input data-bind="datepicker: profile.Birthdate()" />
私はここに、この上で見つけたカスタムバインディング使用しています:http://jsfiddle.net/rniemeyer/NAgNV/
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function() {
var observable = valueAccessor();
ko.observable($(element).datepicker("getDate"));
$(element).blur();
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).datepicker("destroy");
});
},
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).datepicker("getDate");
if(value != null)
{
if(value.toString()[0] = "/")
value = new Date(parseInt(value.toString().substr(6)));
}
if (value - current !== 0) {
$(element).datepicker("setDate", value);
}
}
を};
(値.toString()[0] = "/")を入力して、テキストボックスに表示する日付の書式を設定しました。
これは、オブジェクトを保存しようとするときを除いて、サーバーに戻って送信する以外はうまくいくようです。検証するためのアラートを追加しました。サーバーにオブジェクトを送信する前に、日付に変更はありません。
save : function(){
alert(this.profile.Birthdate);
私が間違っていることについてのアイデアはありますか?
ありがとうございます。
お返事ありがとうございました。私は変更を加えましたが、値は変わりません。私はそれが拘束力と関係していると思う。私が次のことをするとさらに悪化する。 data:ko.toJSON(this) 生年月日としてnullのビジネスオブジェクトがあります – Eric
jsfiddleにあるものを投げてもいいですか?私はこの例にsaveメソッドを追加し、更新された値は次のようになります:http://jsfiddle.net/NAgNV/188/ – KodeKreachor
もう一度ありがとうございます!私はそれを見て、それは動作します...しかし、私のソリューションのために動作しません。私の問題は、私のビジネスオブジェクトがC#ビジネスオブジェクトを介してノックアウトになっているという事実に関連していると思います。 C#ビジネスオブジェクトとマッピングプラグインを使用してこれを行いましたか?それを働かせるのは難しいことですか? – Eric