gpt4 book ai didi

javascript - Knockout 无法处理特定函数中的绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 20:45:34 26 4
gpt4 key购买 nike

我是 knockout 的新手,对特定的绑定(bind)有疑问。我正在使用 SharePoint 获取用户属性,显示它们并将其保存到共享点列表。我有其他绑定(bind),它们运行良好。这是我所拥有的:

    <div id="custom-new-form">
<a data-bind="click: $root.AddRow" href="javascript:void(0)">Add</a>
<table id="sales-returns-table" data-bind="visible: EntityRows().length > 0">
<thead>
<tr>
<th>Entity</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr>
<!--<td data-bind="text: ($index() + 1)"></td>-->
<td>
<select class="lookup-select" data-bind="options: $root.Entities, optionsText: 'Title', optionsValue: 'Title', value: entity"></select>
</td>
<td>
<select class="lookup-select" data-bind="options: $root.Roles, optionsText: 'Title', optionsValue: 'Title', value: role"></select>
</td>
<td>
<a data-bind="click: $root.RemoveRow" href="javascript:void(0)">X</a>
</td>
</tr>
</tbody>
</table>
</div>

这是我的js:

    var newUser;
(function (newUser) {
var NewForm;
(function (NewForm){
var LookupValue = (function () {
function LookupValue(id, title) {
this.Id = id;
this.Title = title;
}
return LookupValue;
}());
var EntityRow=(function(){
function EntityRow(parent) {
var _this=this;
this.Parent=parent;
//this.RowId=SP.Guid.newGuid().toString();
this.entity=ko.observable('');
this.role=ko.observable('');
}
return EntityRow;
}());
var Model=(function () {
function Model() {
var _this=this;
this.isid= ko.observable('');
//for user
this.firstName = ko.observable();
this.lastName=ko.observable();
this.posTitle=ko.observable();
this.email=ko.observable();
this.phone=ko.observable();

//for approver
this.approverISID= ko.observable('');
this.approverName=ko.observable();
this.approverposTitle=ko.observable();

//for row
this.EntityRows=ko.observableArray();

//for Enitities
//this.entity=ko.observable(0);

//for Roles
//this.role=ko.observable(0);

//for Entities and Roles
this.Entities=[];
this.Roles=[];
}
Model.prototype.AddRow=function(){
this.EntityRows.push(new EntityRow(this));
};
Model.prototype.RemoveRow = function (row) {
row.Parent.EntityRows.remove(row);
};
return Model;
}());
function GenerateLookupValuesArray(data) {
var itemEnumerator = data.getEnumerator();
var lookupValuesArray = [];
// Add empty row
lookupValuesArray.push(new LookupValue(0, ''));
while (itemEnumerator.moveNext()) {
var listItem = itemEnumerator.get_current();
var lookupValue = new LookupValue(listItem.get_id(), listItem.get_item('Title'));
lookupValuesArray.push(lookupValue);
}
return lookupValuesArray;
}
NewForm.GenerateLookupValuesArray = GenerateLookupValuesArray;
function Init() {
var entitiesPromise = RequestController.getItemsFromList('Entity', '<View><Query></Query></View>', 'Include(Id, Title)');
var rolesPromise = RequestController.getItemsFromList('Roles', '<View><Query></Query></View>', 'Include(Id, Title)');
$.when(entitiesPromise, rolesPromise).done(function (entityResult, roleResult) {
var model=new Model();
model.Entities = GenerateLookupValuesArray(entityResult);
model.Roles = GenerateLookupValuesArray(roleResult);
ko.applyBindings(model, $('#custom-new-form')[0]);
$('#custom-new-form').show();
});
}
NewForm.Init = Init;
})(NewForm = newUser.NewForm || (newUser.NewForm = {}));
})(newUser || (newUser = {}));
(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(newUser.NewForm.Init, 'sp.js');
})();

我收到错误 Uncaught ReferenceError: Unable to process binding "value: function (){return entity }"。请注意,如果我将实体作为可观察对象放入模型中,效果会很好。但我需要它作为 SalesReturnRow 的一部分,这样我就可以将它插入一个数组并供以后使用。

最佳答案

事实上,您正在尝试绑定(bind) entity,但是 entityEntityRow 的属性,您的范围仍然是 模型。很明显,您希望表格显示 EntityRows,但您还没有设置它。你应该:

<tr data-bind="foreach: EntityRows">

关于javascript - Knockout 无法处理特定函数中的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48742661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com