2

私は次のものを持っています:

var CardViewModel = function (data) {
    ko.mapping.fromJS(data, {}, this);
    this.editing = ko.observable(false);
    this.edit = function() {
        debugger;
        this.editing(true);
    };

};


var mapping = {

    'cards': {
        create: function (options) {
            debugger;  // Doesn't ever reach this point unless I comment out the create method below
            return new CardViewModel(options.data);

        }
    },

    create: function(options) {
        //customize at the root level.  
        var innerModel = ko.mapping.fromJS(options.data);
        //debugger;
        innerModel.cardCount = ko.computed(function () {
            //debugger;
            return innerModel.cards().length;
        });

        return innerModel;
    }
};

var SetViewModel = ko.mapping.fromJS(setData, mapping);
debugger;
ko.applyBindings(SetViewModel);

これを実行すると、「cards」メソッドがヒットすることはないため、CardViewModelのこれらの編集プロパティは使用できません。「create」メソッドをコメントアウトすると、そのデバッガーをヒットできますが、両方が必要です。何が起こっているのか分かりますか?

4

1 に答える 1

1

'cards'は有効なJavascript変数名ではありません。一重引用符なしで他のことを試してください。

また、内部関数が内部関数を参照し、外部関数で観察可能なノックアウトが表示されないように、CardViewModelコードを編集する必要があります。this

var CardViewModel = function (data) {
    var self = this;
    ko.mapping.fromJS(data, {}, this);
    this.editing = ko.observable(false);
    this.edit = function() {
        debugger;
        self.editing(true);
    };
};
于 2013-03-24T07:47:18.977 に答える