简体   繁体   English

KnockoutJS-使用简单的选择输入发出绑定

[英]KnockoutJS - Issues binding with a simple select input

I'm new to the forums and learning knockout. 我是新来的论坛和学习淘汰赛。 I'm having issues wrapping my head around how to properly bind a select input and save it to an ajax call. 我遇到了如何正确绑定选择输入并将其保存到ajax调用的问题。 So far I have the 2 way binds working fine using simple inputs. 到目前为止,我已经使用简单的输入使2种方式的绑定正常工作。 Im just looking to get some guidance and how to properly save it to an ajax and retrive data from ajax and populate the data. 我只是想获得一些指导,以及如何正确地将其保存到ajax并从ajax检索数据并填充数据。

Just looking for tips or if anyone has any examples to look at. 只是寻找提示,或者如果有人有任何示例可以查看。 I would really appreciate it. 我真的很感激。

HTML: HTML:

<select data-bind="options: available_exploitants_HPSM" multiple="true"></select>
<input data-bind="value: test">
<input data-bind="value: test2">

Here is my view model: 这是我的视图模型:

incidentViewModel = function IncidentViewModel() {
 var self = this;
 self.incidents = ko.observableArray();
 self.currentIncident = ko.observable();
 available_exploitants_HPSM = ko.observableArray(["1","2","3"]);


self.fetchdata = function() {
 $.getJSON(Incident.BASE_URL+filterlist+orderlist,
 function(data) { 
 if (data.d.results) { 
 self.incidents(data.d.results.map(function(item) {
 return new Incident(item);
 }));
 $('#loading').hide("slow");
 $('#IncidentTable').show("slow");
 }else {
 console.log("no results received from server");
 }
 });
    }


self.saveorupdate = function() {
 var id = this.ID,
 url = Incident.BASE_URL + (id ? '(' + encodeURIComponent(id) + ')' : '');
 console.log(url);
 return $.ajax(url, {
 type: id ? "MERGE" : "POST",
 data: ko.toJSON({
 test: this.Description,
 test2: this.Incident,
 composante: this.composante
 }),
 processData: false,
 contentType: "application/json;odata=verbose",
 headers: "accept": "application/json;odata=verbose","If-Match": "*",
 success: function (data) {
 incidentViewModel.fetchdata();

 } 
 });
 }
}


function Incident(data) {
var self = this;
self.composante = ko.observable(data.composante);
self.test= ko.observable(data.test);
self.test2= ko.observable(data.test2);

}


var incidentViewModel = new IncidentViewModel();
ko.applyBindings(incidentViewModel);

Changes that I would have made: 我将进行的更改:

1) in incidentViewModel : 1)在incidentViewModel

...
return new Incident(item, self);
...

2) move saveorupdate to Incident model and edit it to: 2)将saveorupdate移至“ Incident模型并将其编辑为:

function Incident(data, viewmodel) {
    var self = this;
    self.composante = ko.observable(data.composante);
    self.test = ko.observable(data.test);
    self.test2 = ko.observable(data.test2);
    self.id = data.id // you need id to use below in ajax
    self.saveorupdate = function() {
        var id = self.id, // changed to 'self'
            url = Incident.BASE_URL + (id ? '(' + encodeURIComponent(id) + ')' : '');
        console.log(url);
        return $.ajax(url, {
            type: id ? "MERGE" : "POST",
            data: {
                test: self.test(), //Description,
                test2: self.test2(), //Incident,
                composante: self.composante() // changed to 'self'
            },
            processData: false,
            contentType: "application/json;odata=verbose",
            headers: "accept": "application/json;odata=verbose",
            "If-Match": "*",
            success: function(data) {
                viewmodel.fetchdata();
            }
        });
    }

}

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

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