Not signed are signed knockouts?

I use knockoutjsand I am new to this. I want to change the model data based on the selected drop down list. So in my AppModel I subscribe to the array I want to change. But this does not work ?. Here is my code:

var filteredStocks  = [];
function viewModel(model) {
        this.isGeneral = ko.observable(model.generalStockEnabled);
        this.stocks = ko.observable();;
        if (model.generalStockEnabled === true)
        {
            filteredStocks = $.grep(model.stocks, function (v) {
                return v.sourceID === -1;
            });
        }
        else
        {
            filteredStocks = $.grep(model.stocks, function (v) {
                return v.sourceID !== -1;
            });
        }
        // If drop downlist changed
        var dropDownListSelectedValue = $('#enableGeneratInventorydl :selected').val();
        this.stocks.subscribe(function () {
            if (dropDownListSelectedValue === "True") {
                filteredStocks = $.grep(model.stocks, function (v) {
                    return v.sourceID === -1;
                });
                this.stocks(filteredStocks)
                this.isGeneral(true);
            }
            else
            {
                filteredStocks = $.grep(model.stocks, function (v) {
                    return v.sourceID !== -1;
                });
                this.stocks(filteredStocks)
                this.isGeneral(false);
            }
        }, this);

        this.stocks = ko.observableArray(filteredStocks);

when i change the value of the drop down list. Does stock value remain unchanged?

Any help is appreciated.

+4
source share
1 answer

The problem arises due to the reassignment of the variable stocksto another observable. So you first do:

this.stocks = ko.observable();

And then subscribe to this observable. But later you do:

this.stocks = ko.observableArray(filteredStocks);

stocks . , . . : http://jsfiddle.net/9nGQ9/2/

this.stocks = ko.observable();

this.stocks = ko.observableArray();

this.stocks = ko.observableArray(filteredStocks);

this.stocks(filteredStocks);

+9

All Articles