简体   繁体   English

KnockoutJS不会在“ With”绑定上下文内填充可观察到的$ root

[英]KnockoutJS Not Populating $root observable inside the “With” Binding Context

I've searched and searched for an answer, so hope I haven't duplicated this anywhere. 我已经搜索了答案,所以希望我没有在任何地方重复此内容。

I am using ASP .NET MVC5, with KnockoutJS as my ORM. 我正在使用ASP .NET MVC5,并将KnockoutJS作为我的ORM。

For some reason, data isn't being populated in the DOM when I try to reference back to the ViewModel using the $root binding context (Once inside the "with" binding context) 出于某种原因,当我尝试使用$ root绑定上下文引用一次ViewModel时(在“ with”绑定上下文内),数据没有被填充到DOM中。

The with binding context is declared in a normal mvc view razor page, however I am using the $root binding context inside a partial view which is loaded into the main view. with绑定上下文是在普通的mvc视图剃刀页面中声明的,但是我在加载到主视图的部分视图中使用$ root绑定上下文。

Has anyone had any problems like this or can spot my error? 有没有人遇到这样的问题,或者可以发现我的错误? I will paste my viewmodel and html code below. 我将在下面粘贴我的viewmodel和html代码。

ViewModel 视图模型

var ProfileViewModel = function () {
    var self = this;
    this.Member = ko.observable(); - With Binding to this
    this.SocialNetworks = ko.observableArray();
    this.Skills = ko.observableArray();
    this.SkillsFilter = ko.observable(""); - Trying to access these from root
    this.FilteredSkills = ko.observableArray();
    this.References = ko.observableArray();
    this.Has = function (has_what) {
        if (has_what) {
            if (has_what.length > 0) {
                return true;
            } else {
                return false;
            }
        }

        return false;
    };

    $.getJSON("/doitgrad/api/member/CameronPearce91", function (allData) {
        self.Member(new DoItGrad.Objects.Member(allData, true));
        self.FilteredSkills = ko.computed(function () {
            return ko.utils.arrayFilter(self.Skills(), function (item) {
                var filter = self.SkillsFilter(),
                    doesnthaveskill = (jQuery.inArray(item, self.Member().details.skills()) == -1),
                    containsfiltertext = (item.title().indexOf(filter) > -1);

                if (filter != "") {
                    return (doesnthaveskill && containsfiltertext);
                } else {
                    return doesnthaveskill;
                }
            });
        });
    })

    $.getJSON("/doitgrad/api/skill/", function (allData) {
        var mappedSkills = $.map(allData, function (item) { return new DoItGrad.Objects.Skill(item); });
        self.Skills(mappedSkills);
    });
}

var model = new ProfileViewModel();
ko.applyBindings(model);

MVC View MVC视图

<section id="profile-details" data-bind="with: Member">
<section id="profile-cover">
    <!-- ko if: details.images.cover() == null -->
        <img src="/DoitGrad/Content/images/Profile/default_cover.jpg">
    <!-- /ko -->
    <!-- ko ifnot: details.images.cover() == null --><!-- /ko -->
    <section class="change-cover">Change cover photo</section>
    <section id="profile-picture">
        <!-- ko if: details.images.profile() == null -->
            <img src="/DoitGrad/Content/images/Profile/default_avatar.png">
        <!-- /ko -->
        <!-- ko ifnot: details.images.profile() == null --><!-- /ko -->

        <h2 id="profile-name" data-bind="text: title">Cameron Pearce</h2>
        <section id="profile-username" data-bind="text: details.username">CameronPearce91</section>
    </section>
</section>
<section id="profile-wrapper">
    <section id="profile-about" data-bind="text: description">Since I have been at uni, I believe I have achieved a lot. I took a year out of my studies to do a work placement year with Xerox based in Welwyn Garden City, primarily focusing on developing C# Web Applications on the MVC framework. It was the best thing I could have done for my career I believe, I have certainly learnt a lot.</section>

@Html.Partial("partialname")

Partial View 部分视图

<section class="profile-detail-holder">
    <section class="add" data-form="addSkill">+</section>
    <h2 class="profile-detail-header">Skill Wall</h2>
    <ul id="profile-skillwall" data-bind="foreach: details.skills()"></ul>
</section>

<section class="dialog-form" data-form="addSkill">
    <section class="form-cover grey"></section>
    <section class="form-content">
        <section class="form-wrap">
            <section class="form-close">x</section>
            <header class="form-header">Add Skill</header>
            <section class="form-body">
                <form id="dig-member-addskill" class="area" method="post" action="#">
                    <input type="text" data-bind="text: $root.SkillsFilter" placeholder="Filter list of skills..." class="ui-textbox"></input>
                    <ul data-bind="foreach: $root.FilteredSkills"></ul>
                    <section class="ui-button submit">
                        <input type="submit" value="Add">
                    </section>
                </form>
            </section>
        </section>
    </section>
</section>

If anyone needs anymore information, feel free to ask. 如果有人需要更多信息,请随时询问。

I think I've spotted it, and it's fairly simple: 我想我已经发现了,而且非常简单:

<input type="text" data-bind="text: $root.SkillsFilter" placeholder="Filter list of skills..." class="ui-textbox"></input>

you are using a text-binding on the input field, so updating the input won't change the observable. 您在输入字段上使用了文本绑定,因此更新输入不会更改可观察值。 Use a value-binding instead: 使用值绑定:

<input type="text" data-bind="value: $root.SkillsFilter" placeholder="Filter list of skills..." class="ui-textbox"></input>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM