1

我正在尝试了解淘汰赛js的一些逻辑过程,希望您能帮助我。可能有一个非常原始的问题,但在那种情况下,它们将类似于我的理解水平。

所以,这里是js代码:

LookData = [{
    slName: 'body weight',
    slValue: '75',
    slType: 'kg',
    slDate: '01.02.03'
}, {
    slName: 'body fat',
    slValue: '50',
    slType: '%',
    slDate: '02.03.04'
}];


function LGoal(data) {
    this.alooks = ko.observableArray(data.LookData);
    this.ltitle = ko.observable(data.slName);
    this.lvalue = ko.observable(data.slValue);
    this.ltype = ko.observable(data.slType);
    this.ldate = ko.observable(data.slDate);
}


function GoalListViewModel() {
    var self = this;
    self.lookgoals = ko.observableArray([]);


    var mappedLookGoals = $.map(LookData, function (item) {
        return new LGoal(item);
    });
    self.lookgoals(mappedLookGoals);


}


ko.applyBindings(new GoalListViewModel(LookData));

LookData 数组模仿我们收到的 JSon 数据对象。之后来自数组 LookData 的信息成为 HTML 页面的一部分:

这是一项工作,我可以在 HTML 中看到结果,但这里有几个问题:

  1. 为什么我需要放置“foreach:lookdata”而不是“foreach:LGoal”?
  2. 如何实现将具有可观察数据的新行添加到数组中?

PS:对不起我糟糕的英语

4

2 回答 2

0

如果您浏览以下代码,您将获得问题的答案。

Why I need to put "foreach: lookdata" instead of "foreach: LGoal"?

我做了一些更改,您可以使用“foreach:lookdata”而不是“foreach:LGoal”..

How can I realize the adding of new row with observible data to array?

newLookData 方法用于从您的 json 添加数据。

newEmptyLookData 方法用于向您的 ui 添加空数据。

LookData = [{
 slName: 'body weight',
 slValue: '75',
 slType: 'kg',
 slDate: '01.02.03'
}, {
 slName: 'body fat',
 slValue: '50',
 slType: '%',
 slDate: '02.03.04'
}];

function LGoal(data){
 this.lookdata.push(new newLookData(data));
}

function newLookData(data) {
 this.ltitle = ko.observable(data.slName);
 this.lvalue = ko.observable(data.slValue);
 this.ltype = ko.observable(data.slType);
 this.ldate = ko.observable(data.slDate);
}

//call this method to add empty row
function newEmptyLookData(data) {
 this.ltitle = ko.observable('');
 this.lvalue = ko.observable('');
 this.ltype = ko.observable('');
 this.ldate = ko.observable('');
}

function GoalListViewModel() {
  var self = this;
  self.lookdata= ko.observableArray([]);

  ko.utils.arrayForEach(LookData, function(item) {
    LGoal(item);
  });

}

ko.applyBindings(new GoalListViewModel());
于 2013-03-25T07:22:57.890 回答
0

我没有看到您的代码有任何特殊问题。我会稍微重构一下,然后让 GoalListViewModel 在构造函数中接受它的数据。而且我真的不明白你在 LGoal ViewModel 中要做什么 - 它在评论中。

这是我要做的:

function LGoal(data) {
    //this does not make sense to me. Why the "data" object at this moment 
    //holds one item of your LookData collection - and there is no more 
    //inner LookData collection.
    //this.alooks = ko.observableArray(data.LookData);
    this.ltitle = ko.observable(data.slName);
    this.lvalue = ko.observable(data.slValue);
    this.ltype = ko.observable(data.slType);
    this.ldate = ko.observable(data.slDate);
}


function GoalListViewModel(data) {
    var self = this;
    self.lookgoals = ko.observableArray([]);


    var mappedLookGoals = $.map(data, function (item) {
        return new LGoal(item);
    });
    self.lookgoals(mappedLookGoals);
}

ko.applyBindings(new GoalListViewModel(LookData));
于 2013-03-24T21:37:33.570 回答