最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting Knockout js json to observable - Stack Overflow

programmeradmin0浏览0评论

I am struggling to return JSON data and convert it into an observable. The data is returned fine in JSON format, but it doesn't seem to be assigned to the observable. Can anybody help? I am guessing the issue is in the success part of the ajax call :

<script type="text/javascript">

function StandingsViewModel() {
    var self = this;

    self.standings = ko.observableArray();

    self.DivisionName = ko.observable('');

    self.afceast = koputed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "AFC East" == i.DivisionName;
        });
    });

    self.afccentral = koputed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "AFC Central" == i.DivisionName;
        });
    });

    self.afcwest = koputed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "AFC West" == i.DivisionName;
        });
    });

    self.nfceast = koputed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "NFC East" == i.DivisionName;
        });
    });

    self.nfccentral = koputed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "NFC Central" == i.DivisionName;
        });
    });

    self.nfcwest = koputed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "NFC West" == i.DivisionName;
        });
    });

    $.ajax({
        dataType: "json",
        url: "/api/standing/GetStandingsBySeason/2018",
        beforeSend: function (xhr) {
            $('#divStandings').html('');
            $('#divStandings').addClass('ajaxRefreshing');
            xhr.setRequestHeader('X-Client', 'jQuery');
        },
        success: function (result) {
            $('#divStandings').removeClass('ajaxRefreshing');
            self.standings(JSON.parse(result));
        }
    });
}

$(document).ready(function () {
    ko.applyBindings(new StandingsViewModel());
});

</script>

I am struggling to return JSON data and convert it into an observable. The data is returned fine in JSON format, but it doesn't seem to be assigned to the observable. Can anybody help? I am guessing the issue is in the success part of the ajax call :

<script type="text/javascript">

function StandingsViewModel() {
    var self = this;

    self.standings = ko.observableArray();

    self.DivisionName = ko.observable('');

    self.afceast = ko.computed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "AFC East" == i.DivisionName;
        });
    });

    self.afccentral = ko.computed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "AFC Central" == i.DivisionName;
        });
    });

    self.afcwest = ko.computed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "AFC West" == i.DivisionName;
        });
    });

    self.nfceast = ko.computed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "NFC East" == i.DivisionName;
        });
    });

    self.nfccentral = ko.computed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "NFC Central" == i.DivisionName;
        });
    });

    self.nfcwest = ko.computed(function () {

        return ko.utils.arrayFilter(self.standings(), function (i) {
            return "NFC West" == i.DivisionName;
        });
    });

    $.ajax({
        dataType: "json",
        url: "/api/standing/GetStandingsBySeason/2018",
        beforeSend: function (xhr) {
            $('#divStandings').html('');
            $('#divStandings').addClass('ajaxRefreshing');
            xhr.setRequestHeader('X-Client', 'jQuery');
        },
        success: function (result) {
            $('#divStandings').removeClass('ajaxRefreshing');
            self.standings(JSON.parse(result));
        }
    });
}

$(document).ready(function () {
    ko.applyBindings(new StandingsViewModel());
});

</script>
Share Improve this question edited Mar 13, 2015 at 14:27 Wrench 4,9205 gold badges35 silver badges49 bronze badges asked Feb 24, 2013 at 12:26 user517406user517406 13.8k30 gold badges84 silver badges122 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

You should use Knockout Mapping plugin and map your result to observable.

var observableData = ko.mapping.fromJS(result);

or if your object wasn't parsed automatically by jQuery

var observableData = ko.mapping.fromJSON(result);

If your data type is array it will be converter to observableArray, so to get it items as normal array you should get like from any other observable by adding brackets;

var array = observableData();

That array can by assigned to your obsevableArray in that way:

self.standings(array);

An alternative option to using the mapping plugin for Knockout would be to use Knockback. It is a bridge between Knockout and Backbone.

You easily get your data like so:

//Model
var StandingsModel = Backbone.Collection.extend({ 
    url:'/api/standing/GetStandingsBySeason/2018' 
});

//View model
var StandingsViewModel = function (standings) {
    this.standings = kb.collectionObservable(standings)
    //...
};

$(document).ready(function () {
    //Get data from server
    var model = new StandingsModel();
    model.fetch( function() {
        success: //...
    });

    //Apply bindings
    ko.applyBindings(new StandingsViewModel(model));
});
发布评论

评论列表(0)

  1. 暂无评论