Knockout无法处理特定函数中的绑定

时间:2018-02-12 09:00:06

标签: javascript jquery knockout.js

我是淘汰赛的新手,并且遇到特定绑定问题。我使用SharePoint来获取用户属性,显示它们并将其保存到sharepoint列表。我有其他绑定,他们运作良好。这就是我所拥有的:

    <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:无法处理绑定“value:function(){return entity}”。请注意,如果我将实体作为可观察的实体放入模型中,它就可以正常工作。但我需要它作为SalesReturnRow的一部分,所以我可以将它推入一个数组并在以后使用它。

1 个答案:

答案 0 :(得分:1)

实际上,您正在尝试绑定entity,但entityEntityRow的属性,而您的范围仍为Model。很明显,您希望您的表格显示EntityRows,但您还没有设置它。你应该:

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