【问题标题】:KnockoutJS/Bootstrap - Clearing modal form when closing modal using javascriptKnockoutJS/Bootstrap - 使用 javascript 关闭模式时清除模式表单
【发布时间】:2013-05-10 12:08:27
【问题描述】:

我有一个引导模式,其中包含用于更新或创建实体的表单(在我的示例中为公司)。现在我的问题是,如果我使用模式查看实体,当我以任何方式关闭模式时,它不会清除字段。如果我然后单击“创建”按钮,导致表单仍然被填充,这应该会显示一个空白模式。

如何仅从常规 javascript 执行我的 ViewModels 方法之一?这是我的代码的一些

function ViewModel() {
        var self = this;

       function CompanyViewModel(company) {
            var self = this;
            self.Id = company.CompanyId;
            self.Name = company.Name;
        }

        function BlankCompanyViewModel() {
            var self = this;
            self.Id = 0;
            self.Name = "";
        }

        self.company = ko.observable();
        self.companies = ko.observableArray();


        self.clearCurrentCompany = function() {
            self.company(new BlankCompanyViewModel());
        };

       // Initialize the view-model
        $.getJSON("/api/company", function(companies) {
            $.each(companies, function(index, company) {
                self.companies.push(new CompanyViewModel(company));
            });
            self.clearCurrentCompany();
        });
    }

理想情况下,我想在模态的“隐藏”事件上运行 ViewModel.clearCurrentCompany,如下所示:

 $('#myModal').on('hidden', function() {
       //Do something here, not sure what
    });

【问题讨论】:

    标签: javascript .net twitter-bootstrap mvvm knockout.js


    【解决方案1】:

    我喜欢在模态框周围使用自定义绑定,使其基于填充/清除 observable 来打开/关闭/显示。

    类似:

    ko.bindingHandlers.modal = {
        init: function(element, valueAccessor, allBindings, vm, context) {
            var modal = valueAccessor();
            //init the modal and make sure that we clear the observable no matter how the modal is closed
            $(element).modal({ show: false, backdrop: 'static' }).on("hidden.bs.modal", function() {
                if (ko.isWriteableObservable(modal)) {
                    modal(null);
                }
            });
    
            //apply the template binding to this element
            ko.applyBindingsToNode(element, { with: modal }, context);
    
            return { controlsDescendantBindings: true };
        },
        update: function(element, valueAccessor) {
            var data = ko.utils.unwrapObservable(valueAccessor());
            //show or hide the modal depending on whether the associated data is populated
            $(element).modal(data ? "show" : "hide");
        }
    };
    

    然后您将其用于可观察对象。它就像with 绑定到该可观察对象,并根据是否填充可观察对象来显示/隐藏模式。

    这里是一个示例,它显示了它的使用情况,并设置了一个订阅,当模式关闭时,您可以在其中运行自定义代码。 http://jsfiddle.net/rniemeyer/uf3DF/

    【讨论】:

      【解决方案2】:
      function ViewModel() {
          var self = this;
          // your previous code
          $('#myModal').on('hide', function() {
             self.clearCurrentCompany();
          });
      }
      

      就这样。请注意,您要隐藏,而不是隐藏,因为隐藏仅在模态完全消失后才会触发。如果用户在前一个视图关闭之前打开创建,它仍然会被填充。

      【讨论】:

      • self.clearCurrentCompany();不会工作,因为它在 ViewModel 的上下文之外。
      • 另一个有趣的选择是通过淘汰订阅处理模式的可见性。您可以将模式显示绑定到公司事件: self.company.subscribe(function(company) { if (company) { $('#myModal').modal('show'); } else { $('#myModal' ).modal('隐藏'); } });解决方案比这更复杂,这绝不是更好的解决方案,只是不同的范式和看待程序结构的不同方式。
      • 不是。变量 self 已经为 ViewModel() 的作用域定义了,所以它仍然定义在 onHide 函数中,只要 onHide 函数定义在 viewModel 中并且 self 没有在子作用域中重新定义即可。
      • 谢谢马修,它有效。我应该能够弄清楚这一点。有时谈论它会有所帮助。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多