admin管理员组文章数量:1429475
Similar to this post...
This is my property view model:
function propertyViewModel() {
var self = this;
self.propertyTypeList = ko.observableArray([]);
self.selectedPropertyType = ko.observable();
}
This is my model for property type:
function propertyTypeModel(id, name) {
this.Id = ko.observable(id);
this.Name = ko.observable(name);
}
I fetch the data from the database with signalR and call the following client-side functions on success:
connection.client.populatePropertyTypeList = function (propertyTypeList) {
var mappedTypes = $.map(propertyTypeList, function (item) {
return new propertyTypeModel(item.Id, item.Name);
});
self.propertyTypeList(mappedTypes);
};
and:
connection.client.populatePropertyDetails = function (property) {
self.selectedPropertyType(new propertyTypeModel(property.PropertyType.Id, property.PropertyType.Name));
};
First one populates the observable array with all possible property types and second one gets the relevant property type and binds it to selectedPropertyType.
Everything works as expected until I try to introduce a drop-down list and pre-populate it with the name of selectedPropertyType as follows:
<select data-bind="options: propertyTypeList, optionsText: 'Name', value: selectedPropertyType"></select>
This makes my observable object (selectedPropertyType) change its value to the first available option on the list. My understanding is that while this list is rendered propertyTypeList is not populated yet and causes selectedPropertyType to default to the first available value, but then why does Knockout not update the object while calling connection.client.populatePropertyDetails?
I can bind a select list to the Id if I introduce a new object holding the Id only, however I would like to have it bound to the whole selectedPropertyType object. Is it possible?
Similar to this post...
This is my property view model:
function propertyViewModel() {
var self = this;
self.propertyTypeList = ko.observableArray([]);
self.selectedPropertyType = ko.observable();
}
This is my model for property type:
function propertyTypeModel(id, name) {
this.Id = ko.observable(id);
this.Name = ko.observable(name);
}
I fetch the data from the database with signalR and call the following client-side functions on success:
connection.client.populatePropertyTypeList = function (propertyTypeList) {
var mappedTypes = $.map(propertyTypeList, function (item) {
return new propertyTypeModel(item.Id, item.Name);
});
self.propertyTypeList(mappedTypes);
};
and:
connection.client.populatePropertyDetails = function (property) {
self.selectedPropertyType(new propertyTypeModel(property.PropertyType.Id, property.PropertyType.Name));
};
First one populates the observable array with all possible property types and second one gets the relevant property type and binds it to selectedPropertyType.
Everything works as expected until I try to introduce a drop-down list and pre-populate it with the name of selectedPropertyType as follows:
<select data-bind="options: propertyTypeList, optionsText: 'Name', value: selectedPropertyType"></select>
This makes my observable object (selectedPropertyType) change its value to the first available option on the list. My understanding is that while this list is rendered propertyTypeList is not populated yet and causes selectedPropertyType to default to the first available value, but then why does Knockout not update the object while calling connection.client.populatePropertyDetails?
I can bind a select list to the Id if I introduce a new object holding the Id only, however I would like to have it bound to the whole selectedPropertyType object. Is it possible?
Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked May 17, 2013 at 8:45 JerryJerry 1,7825 gold badges30 silver badges42 bronze badges 01 Answer
Reset to default 6You create a new object in your populatePropertyDetails
callback.
Even though it has the same property values for this.Id
and this.Name
as the corresponding object in propertyTypeList
, it is not the same object.
Try changing your callback to this:
connection.client.populatePropertyDetails = function (property) {
var type = property.PropertyType,
list = self.propertyTypeList();
var foundType = ko.utils.arrayFirst(list, function(item) {
return item.Id() == type.Id && item.Name() == type.Name;
});
if(foundType) {
self.selectedPropertyType(foundType);
}
};
本文标签: javascriptKnockoutJSBinding a select option to an objectStack Overflow
版权声明:本文标题:javascript - KnockoutJS - Binding a select option to an object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745477688a2660037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论