javascript - KnockoutJS - Binding a select option to an object - Stack Overflow

Similar to this post...This is my property view model:function propertyViewModel() {var self = this;sel

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 0
Add a ment  | 

1 Answer 1

Reset to default 6

You 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);
    }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477689a4629421.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信