選定值我沒有在過去幾年與knockoutjs工作,我忘記了映射了一些基本的東西,observableArray和autocomplate
我應該保持,從自動完成選定值(JSON)的列表中的數組 我已經創建了下面的代碼:
define(['jquery', 'knockout', 'koAutocomplete', 'mapBase','app/obj/routePoint'],
function ($, ko, koAutocomplete, mapBase, routePoint) {
return function routeBuilder() {
var self = this;
mapBase.initMap();
self.rootPoints = ko.observableArray([new routePoint()]);
self.addNewPoint = function() {
self.rootPoints.push(new routePoint());
};
self.removePoint = function() {
self.rootPoints.remove(this);
};
self.getOptions = function(searchTerm, callback) {
$.ajax({
dataType: "json",
url: "/api/Geocoder",
data: {
query: searchTerm
},
}).done(function (results) {
callback(mapBase.convertToAutoComplate(results));
});
}
};
});
// Main viewmodel class
define(['knockout'],
function (ko) {
return function routePoint(label, locationId) {
var self = this;
self.label = label;
self.locationId = locationId;
self.selectedData = ko.observable();
};
});
<ul data-bind="foreach: { data: rootPoints, as: 'point' } ">
<li>
Name at position <span data-bind="text: $index"> </span>:
<input placeholder="Start Type Name"
data-bind="jqAuto: {
value: $data.selectedData().label,
valueProp: '$data,
source: $parent.getOptions,
dataValue: $data,
inputProp: 'label',
labelProp: 'label',
options: { minLength: 3 } }" />
<a href="#" data-bind="click: $parent.removePoint">Remove</a>
</li>
</ul>
,結果我看到了我的選擇,在用戶界面,但rootPoints陣列可控空值。 如何將選定的對象映射到特定的數組索引?
mapBase.convertToAutoComplate返回什麼? –
@JasonSpake routePoint對象列表 –
如果你在那個函數中放置了一個斷點,它會被擊中嗎? –