2

我有一个选择选项,它从 EmailField 获取其初始值,从 allEmailFields 获取其选项:

<select data-bind="options: $parent.allEmailFields, value: EmailField()"></select>

当我更改选择的值时,我的模型不会更新。这不是双向绑定应该处理的事情吗?或者我需要为更改事件编写处理程序?

模块在这里:

    define('mods/fieldmapping', ["knockout", "libs/knockout.mapping", "datacontext", "mods/campaigner", "text!templates/fieldmapping.html", "text!styles/fieldmapping.css"],
function (ko, mapping, datacontext, campaigner, html, css) {
    'use strict';
    var
        fieldMappingItem = function (data) {
            var self = this;
            self.CrmField = ko.observable(data.CrmField);
            self.EmailField = ko.observable(data.EmailField);
        },
        dataMappingOptions = {
            key: function (data) {
                return data.PlatformFieldName;
            },
            create: function (options) {
                return new fieldMappingItem(options.data);
            }
        },
        fieldMappingViewModel = {
            contentLoading: ko.observable(false)
        },
        showFieldMapping = function () {
            campaigner.addStylesToHead(css);
            campaigner.addModalInnerPanel(html);
        },
        init = function (connectionId, connectionName) {            
            fieldMappingViewModel.fieldMappings = mapping.fromJS([]);
            fieldMappingViewModel.allEmailFields = mapping.fromJS([]);            
            fieldMappingViewModel.originatorConnectionName = ko.observable();
            fieldMappingViewModel.originatorConnectionName(connectionName);
            fieldMappingViewModel.saveFieldMappings = function () {
                console.log(ko.toJSON(fieldMappingViewModel.fieldMappings));
                amplify.request("updateExistingFieldMappings", 
                    {
                        cid: connectionId,
                        RequestEntity: ko.toJSON(fieldMappingViewModel.fieldMappings)
                    },
                    function (data) {
                        console.log(data);
                    });
            };
           showFieldMapping();

            amplify.request('getExistingFieldMappings', { cid: connectionId }, function (data) {
                amplify.request("getCampaignerFields", function (breezerData) {
                    mapping.fromJS(breezerData.ResponseEntity, fieldMappingViewModel.allEmailFields);
                });                
                mapping.fromJS(data.ResponseEntity, dataMappingOptions, fieldMappingViewModel.fieldMappings);                
                ko.applyBindings(fieldMappingViewModel, $('#fieldMapping')[0]);
            });
        };
    return {
        init: init,
        fieldMappingViewModel: fieldMappingViewModel,
        html: html,
        css : css
    }
});
4

1 回答 1

6

代替:

<select data-bind="options: $parent.allEmailFields, value: EmailField()"></select>

和:

<select data-bind="options: $parent.allEmailFields, value: EmailField"></select>

如果你想创建双向依赖,那么你应该传入绑定 observable。

PS: http: //knockoutjs.com/documentation/observables.html#observables

于 2013-02-28T16:59:44.683 回答