我一直在为此摔跤,我需要一些帮助。我有一个KO模型和视图模型,我用它来渲染视图。在这种情况下,对象是一个乐队。这支乐队有一系列可观察的专辑。我只是不知道如何从乐队模型中删除一张专辑。
function band(item) {
var self = this;
self.name = ko.observable(item.name);
self.country = ko.observable(item.country);
self.state = ko.observable(item.state);
self.city = ko.observable(item.city);
self.emailAddress = ko.observable(item.emailAddress);
self.albums = ko.observableArray(item.albums);
}
function User() {
var self = this;
self.bands = ko.observableArray([]);
self.singleBand = ko.observable();
//Get Bands from data source and create new models to add to bands array
$.getJSON("/api/band", function (allData) {
$.each(allData, function (index, item) {
self.bands.push(new band(item));
});
});
//function to get a single band from a rendered list
self.getThisBand = function (item) {
self.bands = ko.observableArray(null);
self.singleBand(item);
};
//remove band from singleBands' album array
self.removeAlbum = function (albumToDelete) {
//how to delete album from band model
};
}
ko.applyBindings(new User());
逻辑很简单,我得到一个乐队列表并将它们绑定到UI中的列表(没有probs)。当我点击UI中的乐队名称时,我加载了getThisBand方法并填充了UI(再次没有概率)。我已将singleBand.albums数组绑定到列表,并且具有femoveAlbum onclick函数。传递给函数的数据也是正确的对象。
有什么根本我缺少的吗?
答案 0 :(得分:1)
看起来self.singleBand()
将等于当前频段,因此您可以在removeAlbum
函数中执行以下操作:
//remove band from singleBands' album array
self.removeAlbum = function (albumToDelete) {
var band = self.singleBand();
band.albums.remove(albumToDelete);
};