2014-02-24 104 views
0

我在淘汰赛中遇到了一些麻烦,希望这里的某个人能够帮助。敲除只能第一次工作

我有两个jQuery函数。其中一个使用knockout绑定到页面上的单个元素。另一个绑定到其余元素,然后调用第一个函数。

该数据来自返回Json的AJAX请求。

我遇到的问题是使用pGroups列表。它在第一次正常工作,但当你再次点击它失败并需要刷新才能再次工作。

控制台错误是:NotFoundError:节点未找到

编辑:更新后的代码,以显示进度

jQuery的是:

//Load user data into the action window when a user is selected 
$('.ListUserLink').click(function() { 

    var url = '@Url.Action("DisplayUser", "AjaxUser")' + '?UserId=' + $(this).attr("UserId") + '&UserNum=' + $(this).attr("UserNum") + "&SectId=" + $(this).attr("Sect"); 

    $.ajax({ 
     url: url, 
     contentType: "application/json; charset=utf-8", 
     type: 'POST', 
     context: this, 
     timeout: 60000, 
     dataType: 'json', 
     tryCount: 0, 
     retryLimit: 3, 
     success: function (data) { 
      //ko.applyBindings(new UserViewModel(data)); 
      viewModel.user = new userModel(data); 
     }, 
     error: function (httpRequest, textStatus, errorThrown) { 
      alert("Error"); 
     } 
    }); 
}); 

//Load sections on department index change 
$("#ddbDepartments").change(function() { 
    var url = '@Url.Action("GetSectionsByDept", "AjaxUser")' + '?deptId=' + $(this).val(); 
    $.ajax({ 
     url: url, 
     contentType: "application/json; charset=utf-8", 
     type: 'POST', 
     context: this, 
     timeout: 60000, 
     dataType: 'json', 
     tryCount: 0, 
     retryLimit: 3, 
     success: function (data) { 
      //ko.applyBindings(new SectionViewModel(data), $(".SectionsDDB")[0]); 
      viewModel.sections = new userModel(data); 
     }, 
     error: function (httpRequest, textStatus, errorThrown) { 
      alert("Error"); 
     } 
    }); 
}); 



//Assign Section details to fields 
function sectionsModel(data) { 
    this.sectionList = ko.observableArray(data.SectionList); 
//  this.sections = this.sectionList; 
//  this.selectedItem = parseInt($("#OldSectionId").value); 
}; 

//Assign user details to fields 
function userModel(data) { 

    this.fullName = ko.observable(data.FirstName + " " + data.Surname); 

    this.firstName = ko.observable(data.FirstName); 
    this.surname = ko.observable(data.Surname); 
    this.usernum = ko.observable(data.UserNum); 

    //Assign JobTitle Dropdown and selected value 
    this.jobTitlesList = ko.observableArray(data.TitlesList); 
    this.jobTitles = this.jobTitlesList; 
    this.selectedItem = data.JobTitleNum; 

    //Assign Group/Application list 
    this.pGroups = ko.observableArray(data.GroupList); 

    this.sections = ko.observableArray([{}]); 

    this.ext = ko.observable(data.Ext); 
    this.userId = ko.observable(data.UserId); 
    this.olduserid = ko.observable(data.UserId); 
    $("#ddbDepartments").val(data.DeptId); 
    this.oldsectionid = ko.observable(data.SectionId); 
    $("#ddbDepartments").change(); 
    this.oldsectionid = ko.observable(data.SectionId); 
    //$("#SectionsDDB").val(data.SectionId); 
}; 

var wrapper = function() { 
    this.user = new userModel(userdata); 
    this.sections = new sectionsModel(sectiondata); 
}; 

var viewModel = new wrapper(); 
    ko.applyBindings(viewModel); 

的pGroups HTML这是在第二失败尝试是:

<div data-bind="with: user" id="ActionWindow"> 

<form action="@Url.Action("SaveUserDetails", "AJAXUser")" method="post" class="AjaxSubmit" id="userDetailsForm"> 
    <h2>User: <span data-bind="text: fullName"></span></h2> 
    <table> 
     <tr> 
      <td>First Name:</td> 
      <td><input type="text" name="FirstName" id="FirstName" data-bind="value: firstName" /></td> 
      <td> 
       <input type="hidden" name="UserNum" id="UserNum" data-bind="value: usernum" /> 
       <input type="hidden" name="OldUserId" id="OldUserId" data-bind="value: olduserid" /> 
       <input type="hidden" name="OldSectionId" id="OldSectionId" data-bind="value: oldsectionid" /> 
      </td> 
      <td></td> 
     </tr> 

     <tr> 
      <td>Surname:</td> 
      <td><input type="text" name="Surname" id="Surname" data-bind="value: surname" /></td> 
      <td></td> 
      <td></td> 
     </tr> 

     <tr> 
      <td>Job Title:</td> 
      <td><select name="JobTitleNum" id="TitlesList" data-bind="options: jobTitles, optionsValue: 'TitleId', optionsText: 'Title', value: selectedItem"></select></td> 
      <td></td> 
      <td></td> 
     </tr> 

     <tr> 
      <td>Extension:</td> 
      <td><input type="text" name="Ext" id="Ext" data-bind="value: ext" /></td> 
      <td></td> 
      <td></td> 
     </tr> 

     <tr> 
      <td>Login ID:</td> 
      <td><input type="text" name="UserId" id="UserId" data-bind="value: userId" /></td> 
      <td></td> 
      <td></td> 
     </tr> 

     <tr> 
      <td>Department:</td> 
      <td> 
       <select id="ddbDepartments" name="DeptId"> 
        @foreach (var d in Model.DepartmentList) 
        { 
         <option value="@d.DeptId">@d.DeptName</option> 
        } 
       </select> 
      </td> 
      <td>Section: </td> 
      <td> 
       <select name="SectionId" class="SectionsDDB" data-bind="options: $root.sections.list, optionsValue: 'SectId', optionsText: 'SectName', value: SectionId"></select> 
       @*<select name="SectionId" class="SectionsDDB" data-bind="options: sections, optionsValue: 'SectId', optionsText: 'SectName', value: selectedItem"></select>*@ 
      </td> 
     </tr> 
    </table> 
    <input type="submit" value="Update User" /> 
    <br /> 
</form> 

<h2>Current Groups</h2> 
<table> 
    <tbody data-bind="foreach: pGroups"> 
     <tr> 
      <td data-bind="text:AppName"></td> 
      <td data-bind="text:GroupName"></td> 
     </tr> 
    </tbody> 
</table> 

其他所有工作。

我发现这一点:http://knockoutjs.com/documentation/plugins-mapping.html

这表明我应该是这样的映射:

var viewModel = ko.mapping.fromJS(data, mapping); 

但尝试,因为我可能我无法弄清楚如何将其应用到我的代码。

任何帮助将不胜感激。

+0

的ko.applyBindings功能不应该叫每次改变时间部门。 每次调用ko.applyBindings时,整个DOM都会检查绑定。因此,如果您多次执行此操作,您将获得同一元素的多个绑定。 请检查此链接,以确保您了解如何在同一页面上拥有多个视图:http://stackoverflow.com/questions/8676988/example-of-knockoutjs-pattern-for-multi-view-applications/8680668#8680668 –

+0

如果你不能解决问题,请添加一个jsfiddle –

+0

所以我需要从AJAX成功移除ko.apply绑定,并将其映射到那里呢?你的链接中的例子似乎是单独调用Dom的单独部分,因为我有一些重叠。 这里是一个jsFiddle,但我不能为我的生活得到它的工作http://jsfiddle.net/dariune/8djDw/ –

回答

1

基于我在以前的评论中提供的链接(http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html)我做你的可能视图模型的一个例子:

//the overall model 
var wrapper = function() { 
    this.user = ko.observable(); 
    this.user(new userModel(userdata)); 
    this.sections = new sectionsModel(sectiondata); 
}; 
var viewModel = new wrapper(); 
ko.applyBindings(viewModel); 

然后在$(“ ListUserLink”)成功点击你可以有:

viewModel.user(new UserViewModel(data)); 

而且在$( “#ddbDepartments”)的成功改变,你可以有:

viewModel.sections.sectionList(data.SectionList); 

在视图中,可以使用与$根一起绑定来绑定一个嵌套的视图模型:

<div data-bind="with: user"> 
     <h2>User: <span data-bind="text: fullName"></span></h2> 
     ... 
     <select name="SectionId" class="SectionsDDB" data-bind="options: $root.sections.sectionList, optionsValue: 'SectId', optionsText: 'SectName', value: $root.sections.selectedValue"></select> 
     ... 
    </div> 
+0

谢谢我已经做了链接说,我想我几乎在那里,这一次我失去了一些愚蠢的东西。 我收到错误:无法解析绑定。为fullName(我想其余的)。 我已更新我的jsfiddle以显示此:http://jsfiddle.net/dariune/8djDw/2/ –

+0

检查了这一点:http://jsfiddle.net/8djDw/3/ –

+0

我做了一个改变:http: //jsfiddle.net/8djDw/5/。我还添加了jquery库,以便能够设置ddbDepartments的值并将SectionId属性添加到userModel –

0

据我所知,这些部分是根据部门选择加载的。那么,为什么你不只是添加sectionList observableArray到UserViewModel,只是添加订阅部门ID,所以每次的变化部门,部分列表加载:

this.DeptId = ko.observable(data.DeptId); 
this.sectionList = ko.observableArray(GetSections(this.DeptId())); 
this.DeptId.subscribe(function() { 
       var newSections = GetSections(this.DeptId()); 
       this.sectionList(newSections); 
      }, this); 

GetSections功能应该叫你GetSectionsByDept方法。 在你的jsfiddle,如果你发表意见follwing线,数据将加载:

$("#ddbDepartments").val(data.DeptId); 
$("#ddbDepartments").change(); 
$("#SectionsDDB").val(data.SectionId); 

你也应该更换这个下拉列表部门:

<select name="DeptId" id="ddbDepartments" data-bind="options: DepartmentList, optionsValue: 'Id', optionsText: 'DeptName', value: DeptId"></select> 

但要确保有部门的数组具有Id/DeptName属性的对象。

+0

嗨,谢谢你的回复。 ddbdepartments在页面加载时加载,数据来自控制器。所以它需要处于目前的状态。是的,你的大部分都是正确的。点击一个按钮(其中有很多)将调用ListUserLink AJAX方法,然后调用UserViewModel,一旦部门值被选中,将调用SectionViewModel显示这些部分。一旦完成,您可以选择一个只调用SectionViewModel的部门。所以我保持他们独立的ListUserLink数据不包含部分信息。那有意义吗? –

+0

是的,但你仍然会有重叠的绑定,因为当你调用ko.applyBindings(new UserViewModel(userdata));它也将适用于SectionsDDB。请检查此链接以查看处理方法:http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html。 –

0

你不需要实例一个新的ViewModel总是你有一个Ajax服务器响应。 您可以在页面准备就绪时应用绑定,并在ajax完成时更新数据更改,或者您可以删除viewmodel绑定并再次应用到您的ajax中。

+0

感谢您的回复。如何在没有新视图模型的情况下做到这一点? –

+0

在成功更改事件时,替换“ko.applyBindings(new SectionViewModel(data),$(”.SectionsDDB“)[0]);”到“yourViewModelInstance.secionList(data.SectionList);” –

+0

我不应该首先声明viewModel吗?我已经尝试通过将其更改为\t SectionViewModel.sectionList(data.SectionList);但那不起作用。说SectionViewModel.sectionList不是一个函数,这是我所期望的,因为它没有声明一个新的实例。 –