选择的下拉值更改时,可观察到的淘汰赛未更新

5号

我们一直在和和Chosen一起使用一切工作正常,直到我们从切换现在使用捆绑。问题是,当我们更改下拉列表中的值时,不会更新RequireJsKnockOutRequireJScommonjswebpackknockout observablechosen

这是使用的javascript代码RequireJs

define(['knockout', 'text!./employee-setup.html', 'utils', 'panel-section', 'toastr', 'jquery', 'knockout-postbox', 'knockout-projections', 'chosen', 'jsteps'], function (ko, template, utils, PanelSection, toastr, $, _, _, _, jsteps) {
function EmployeeSetup(params) {
    var self = this;
    this.agentTypes = ko.observableArray();
    this.agentType = ko.observable();


    this.loadAgentTypes = function () {
        $.ajax({
            url: '/Employee/GetAgentTypes',
            method: 'POST',
            dataType: 'json',
            success: function (result) {
                if (utils.handleAjaxResult(result) && result.Data) {
                    self.agentTypes([]);

                    var agentType = [{ ID: "", Name: "" }];

                    $.each(result.Data, function (i, item) {
                        agentType.push({ID: item.ID, Name: item.Name});
                    });
                    self.agentTypes(agentType);
                    $('#agentType').chosen({ allow_single_deselect: true, width: '310px' });
                    $('#agentType').trigger("chosen:updated");
                } else {
                }

            },
            error: function () {
                toastr.error('Could not load agent types');
            }
        });
    };
    self.loadAgentTypes();
    };
 return { template: template, viewModel: EmployeeSetup };
});

该组件的html:

<div class="input-container" data-bind="">
     <select data-bind="value: agentType, options: agentTypes, optionsText: 'Name'" data-placeholder="Select Agent Type..." id="agentType" class="chosen-select sp-uin-dropdown" tabindex="2"> </select>
</div>

这是使用的代码 commonjs

var ko = require('knockout'),
    utils = require('utils'),
    PanelSection = require('panel-section'),
    toastr = require('toastr'),
    $ = require('jquery');
require('knockout-postbox');

function ViewModel(params) {
   var self = this;
   this.agentTypes = ko.observableArray();
   this.agentType = ko.observable();

   this.loadAgentTypes = function () {
   $.ajax({
       url: '/Employee/GetAgentTypes',
       method: 'POST',
       dataType: 'json',
       success: function (result) {
       if (utils.handleAjaxResult(result) && result.Data) {
              self.agentTypes([]);

              var agentType = [{ ID: "", Name: "" }];

              $.each(result.Data, function (i, item) {
                  agentType.push({ID: item.ID, Name: item.Name});
              });
              self.agentTypes(agentType);
              $('#agentType').chosen({ allow_single_deselect: true, width: '310px' });
              $('#agentType').trigger("chosen:updated");
            } else {
           }
        },
        error: function () {
           toastr.error('Could not load agent types');
        }
    });
  };
  self.loadAgentTypes();
}
module.exports = { viewModel: ViewModel, template: require('./template.html')      };

它使用的是与html上述相同的文件。

在中,webpack.config.js我们定义了到jquery的路径chosen

它加载chosen dropdown正确。但是,当我subscribe观察到时,下拉列表更改时不会更新值。初始加载时,我只能从控制台看到一次该值。

self.agentType.subscribe(function (value) {
    console.log('value', value);
}, this)

因此,很少有文章建议使用bindingHandlers我已经在应用程序中尝试了JSFiddle的工作代码,但仅从初始加载中获取了该值。

关于如何解决此问题或造成此问题的任何建议?

5号

此问题是由引起的webpack为了解决这个问题,我的同事写了一个习惯bindingHandler

HTML 代码:

<div class="input-container">
 <select data-bind="
                value: agentType,
                options: agentTypes, 
                optionsText: 'Name',
                dropdown: {
                    width: '310px',
                    allow_single_deselect: true
                } "
                data-placeholder="Select Agent Type..." id="agentType">
</select>

自定义bindingHandler

// a dropdown handler, which currently utilizes the Chosen library

ko.bindingHandlers.dropdown = {

    init: function(element, valueAccessor, allBindings, viewModel, bindingContext){

        // get chosen element

        var $element = $(element);

        // get options (if any) to pass to chosen when creating

        var options = ko.unwrap(valueAccessor());



        // NOTE: when using Chosen w/ webpack, the knockout bindings no longer

        // fired. This event handler is to remedy that. It watches the change

        // event for the underlying <select> (which chosen updates), and

        // updates the corresponding observables mapped to value and selectedOptions.

        // Only one should be bound, value for single select, selectedOptions for multi-select

        // binding direction: Knockout <- Chosen

        $element.on('change', function(e, item) {

            var valueProp = allBindings.has('value') && allBindings.get('value');

            var selectedOptionsProp = allBindings.has('selectedOptions') && allBindings.get('selectedOptions');

            if (item) {

                if (allBindings.has('options')) {

                    var allOptions = ko.unwrap(allBindings.get('options'));

                    if (valueProp) {

                        // single select

                        if (ko.isObservable(valueProp)) {

                            if (!item.isMultiple) {

                                if (item.selecting) {

                                    valueProp(allOptions[item.index]);

                                } else {

                                    valueProp(null);

                                }

                            }

                        }

                    }

                    if (selectedOptionsProp) {

                        // multi select

                        if (ko.isObservable(selectedOptionsProp)) {

                            if (item.isMultiple) {

                                // handle multi select

                                if (item.selecting) {

                                    // select

                                    selectedOptionsProp.push(allOptions[item.index]);

                                } else {

                                    // deselect

                                    selectedOptionsProp.remove(allOptions[item.index]);

                                }

                            }

                        }

                    }

                }

            } else {

                // this is triggered w/o args when the control is reset. This happens when deselecting during single-select

                if (valueProp) {

                    // single select

                    if (item === undefined && ko.isObservable(valueProp)) {

                        valueProp(null);

                    }

                }

            }

        });
        // handle updating the chosen component's UI when the underlying

        // options, selectedOptions or value changes

        // binding direction: Knockout -> Chosen

        ['options', 'selectedOptions', 'value'].forEach(function(propName){

            if (allBindings.has(propName)){

               var prop = allBindings.get(propName);

                if (ko.isObservable(prop)){

                    //console.log('subscribing to:', propName, ' for:', $element);

                    prop.subscribe(function(value){

                        if (value != null) {

                            //console.log('calling chosen:updated');

                            var options = ko.unwrap(allBindings.get('options'));

                            // console.log('got options:', options);

                            if (options) {

                                if (options.indexOf(value) > -1) {

                                    // item is in options

                                    //        console.log('value is in options:', value);

                                } else {

                                    // item is not in options, try to match ID

                                    options.some(function (item) {

                                        if (item.ID == value) {

                                            // update the obs. to the entire item, not the ID

                                            prop(item);

                                        }

                                    });

                                }

                            }

                        }

                        $element.trigger('chosen:updated');

                    });

                }

            }

        });       

        // add chosen css class (not sure this is needed)

        $element.addClass('chosen-select');



        // create chosen element, passing in options if any were specified

       if (typeof options === 'object') {

            $element.chosen(options);

        } else {

            $element.chosen();

        }

        $element.trigger('chosen:updated');

    }

};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

选择的下拉值更改时,可观察到的淘汰赛未更新

来自分类Dev

可观察到淘汰赛更新

来自分类Dev

淘汰赛可观察到的数组,更改事件获得更新的项目值

来自分类Dev

当其ViewModel可观察到的更改时,淘汰表组件视图未更新

来自分类Dev

淘汰赛计算可观察到的参数

来自分类Dev

淘汰赛可观察到的表演

来自分类Dev

淘汰赛移除率从可观察到的限制

来自分类Dev

淘汰赛可观察到的最新价值

来自分类Dev

淘汰赛-添加了可观察到的不更新新对象的功能

来自分类Dev

淘汰赛-添加了可观察到的未在新对象上更新的功能

来自分类Dev

可观察到淘汰赛-为什么此值会自动更新?

来自分类Dev

淘汰赛:更改可观察值

来自分类Dev

浏览器之间可观察到的淘汰赛共享-仅在本地更改值时订阅

来自分类Dev

使用ko.mapping.fromJS更新异步ajax调用后可观察到的淘汰赛

来自分类Dev

更新扩展程序中可观察到的淘汰赛js时如何保留光标位置

来自分类Dev

当儿童可观察到的更新时,淘汰赛js css似乎没有重新计算

来自分类Dev

通过访问observableArray计算出可观察到的淘汰赛

来自分类Dev

可观察到的淘汰赛:手动应用扩展剂

来自分类Dev

从可观察到的淘汰赛中删除最后一个角色

来自分类Dev

用FuelUX树订阅可观察到自定义Jquery事件的淘汰赛

来自分类Dev

淘汰赛:可观察到,无默认值

来自分类Dev

为什么可观察到的不通知是否阻止了淘汰赛?

来自分类Dev

从可观察到的淘汰赛中删除最后一个角色

来自分类Dev

将recaptcha响应放入可观察到的js淘汰赛中

来自分类Dev

淘汰赛无法观察到样式属性

来自分类Dev

在淘汰赛js中,单选按钮的可观察值未更改

来自分类Dev

使用可观察数组计算出的淘汰赛未更新

来自分类Dev

淘汰赛3.2 AMD组件未更新可观察对象

来自分类Dev

自动填充表单表单(Norton Identity Safe)未更新“淘汰赛”的可观察值

Related 相关文章

  1. 1

    选择的下拉值更改时,可观察到的淘汰赛未更新

  2. 2

    可观察到淘汰赛更新

  3. 3

    淘汰赛可观察到的数组,更改事件获得更新的项目值

  4. 4

    当其ViewModel可观察到的更改时,淘汰表组件视图未更新

  5. 5

    淘汰赛计算可观察到的参数

  6. 6

    淘汰赛可观察到的表演

  7. 7

    淘汰赛移除率从可观察到的限制

  8. 8

    淘汰赛可观察到的最新价值

  9. 9

    淘汰赛-添加了可观察到的不更新新对象的功能

  10. 10

    淘汰赛-添加了可观察到的未在新对象上更新的功能

  11. 11

    可观察到淘汰赛-为什么此值会自动更新?

  12. 12

    淘汰赛:更改可观察值

  13. 13

    浏览器之间可观察到的淘汰赛共享-仅在本地更改值时订阅

  14. 14

    使用ko.mapping.fromJS更新异步ajax调用后可观察到的淘汰赛

  15. 15

    更新扩展程序中可观察到的淘汰赛js时如何保留光标位置

  16. 16

    当儿童可观察到的更新时,淘汰赛js css似乎没有重新计算

  17. 17

    通过访问observableArray计算出可观察到的淘汰赛

  18. 18

    可观察到的淘汰赛:手动应用扩展剂

  19. 19

    从可观察到的淘汰赛中删除最后一个角色

  20. 20

    用FuelUX树订阅可观察到自定义Jquery事件的淘汰赛

  21. 21

    淘汰赛:可观察到,无默认值

  22. 22

    为什么可观察到的不通知是否阻止了淘汰赛?

  23. 23

    从可观察到的淘汰赛中删除最后一个角色

  24. 24

    将recaptcha响应放入可观察到的js淘汰赛中

  25. 25

    淘汰赛无法观察到样式属性

  26. 26

    在淘汰赛js中,单选按钮的可观察值未更改

  27. 27

    使用可观察数组计算出的淘汰赛未更新

  28. 28

    淘汰赛3.2 AMD组件未更新可观察对象

  29. 29

    自动填充表单表单(Norton Identity Safe)未更新“淘汰赛”的可观察值

热门标签

归档