0

Hi I seem to be having a problem with KO array bindings not applying.. Here is my code:

var movements_array = [];
var viewModel = { 
    movements: ko.observableArray(movements_array),
};
$(document).ready(function(){
    ko.applyBindings(viewModel);
});

function get_alteration_movement(){

    for(var i = 0; i < index.movements.length; i++)
    {
        movements_array[i] = index.movements[i].description;
        console.log(movements_array);

        viewModel.movements(movements_array);
        console.log(viewModel.movements);
    }

}

Index is JSON data fetched from another js page.

4

1 回答 1

0

至于我,我通常使用函数而不是对象文字,这更清楚: 声明为对象文字与函数的淘汰视图模型之间的区别

var viewModel = function(){
    this.movements = ko.observableArray([]);
};

$(document).ready(function () {
    var model = new viewModel();
    ko.applyBindings(model);
    get_alteration_movement(model);
});

function get_alteration_movement(model) {
    var movements_array = [];
    for (var i = 0; i < index.movements.length; i++) {
        movements_array[i] = index.movements[i].description;
        console.log(movements_array);
    }

    model.movements(movements_array);
}
于 2013-06-20T21:14:33.820 回答