Knockoutjs:我如何将可观察数组中的单个项目传递给我的viewmodel?

时间:2014-11-15 04:08:35

标签: javascript knockout.js bootstrap-modal ko.observablearray

我在KnockoutJS中创建了一个错误处理程序,但我遇到了一个问题。我有一个" bug"在我的主页上报告使用可观察数组显示。当我点击"查看"在列表中的一个错误报告中,我弹出一个Bootstrap模式。我想用报告详细信息填充模态的内容,但由于某种原因,视图模型没有正确传递。

这是我的视图模型,以及下面的ajax映射:

    function BugsViewModel(doJSON) { //doJSON determines whether to populate the observable with JSON or leave it empty
        var self = this;
        self.bugs = ko.observableArray();
        self.removeBug = function (bug) {
            $.ajax({
                url: "/ajax/getbugs.ashx?deleteBug=" + bug.Id()
            })
            self.bugs.remove(bug);
        }

        self.currentBug = ko.observable(); // <--this is the bug that I want to reference in the modal
        self.setModalBug = function (obj, event) { //obj is the individual array item
            self.currentBug = obj; //setting currentBug so that I can use it in my view
            alert(self.currentBug.Id()); //<--these alert properly, so I know that the value I'm passing in (obj) is correct
            alert(self.currentBug.BugName());
        }
        if(doJSON)
            getViewModelJSON(self.bugs); //this function loads my array (self.bugs) with observable items
    }
function getViewModelJSON(model) {
        $.ajax({
            url: "/ajax/getbugs.ashx"
        })
    .done(function (data) {
        ko.mapping.fromJSON(data, {}, model);
    });
    }

    $(document).ready(function () {
        viewModel = new BugsViewModel(true);
        ko.applyBindings(viewModel);
    });

我有我的&#34;查看&#34;按钮,调用&#34; setModalBug&#34;并打开模态:

<a href="#" class="btn btn-default" data-toggle="modal" data-target="#bugDetails" data-id="value: Id" data-bind="click: $root.setModalBug">View</a>

然后,在我的详细信息模式中,我有我想要填充数据的文本框。这就是我遇到问题的地方 - currentBug.BugName没有正确填充值。

<input type="text" id="txtBugName" data-bind="textInput: currentBug.BugName" />

(请注意,我正在使用Knockout的Mapping插件,所以即使你没有看到&#34; BugName&#34;在我的viewmodal中定义,当我调用&#时,它是从JSON生成的34; ko.mapping.fromJSON()&#34;。)

我有点糊涂了。这是运行时问题,还是我不正确地调用了viewmodel?还是完全不同的东西?

由于

2 个答案:

答案 0 :(得分:2)

您没有正确地将值分配给observable。你在做什么:

self.currentBug = ko.observable();
self.currentBug = obj;

第一行创建一个observable,但第二行抛出它并用bug对象替换它。你想要分配给observable,而不是扔掉它,比如:

self.currentBug = ko.observable();
self.setModalBug = function (obj, event) {
    self.currentBug(obj); //<-- Assign the current bug the currentBug observable
    alert(self.currentBug().Id()); //note the additional () to read the value of the observable
    alert(self.currentBug().BugName());
}

答案 1 :(得分:0)

我得到了它的工作。显然在我的代码中,我实际上没有在警报中添加额外的'()'。对不起 - 不是我最好的时刻。现在它起作用了:

self.currentBug(obj);
alert(self.currentBug().Id());
alert(self.currentBug().BugName());

然后我在textInput data-bind:

中添加了额外的'()'
<input type="text" id="txtBugName" data-bind="textInput: currentBug().BugName" />

我没有意识到我必须在数据绑定的任何地方使用'()'。我认为你发现你正在使用observable并对其进行处理(它在属性“BugName”上进行处理,但我必须在最后使用'()'访问currentBug。

谢谢,Retsam!我花了很多时间在这上面,这令人沮丧。原来这是一个非常简单的修复。