【问题标题】:Bootstrap model do not closed properly when using with knockout js与淘汰赛 js 一起使用时,引导模式无法正确关闭
【发布时间】:2020-01-16 10:05:33
【问题描述】:

我正在使用带有引导 js 的淘汰赛。

我的要求:

  • 我正在创建一个 HTML 表单,并且我已经使用敲除完成了数据绑定。
  • 提交此表单后,我会以引导模式向用户显示一条消息。
  • 关闭此模式后,我需要重置 HTML 表单。所以点击关闭按钮我并做一个点击绑定并调用一个方法。下面是该方法的 sn-p。

// Re-setting the observable  to default value
me.addStudentForClass(null);
me.parents=ko.mapping.fromJS(new parentInfoModel(undefined));
me.localGuardian=ko.mapping.fromJS(new localGuardian(undefined));
me.lastSchoolEducation=ko.mapping.fromJS(new lastSchoolEducation(undefined));
// Closing bootstrap Modal
$('modal-id').modal('hide');
// Cleaning the knockout node , to reflect the reset change 
ko.cleanNode($('#add-student')[0]);
// and binding the view model 
ko.applyBindings(app.mainViewModel, $('#add-student')[0]);

如果我们没有使用下面两行代码,那么模态工作正常并且模态在单击关闭按钮时关闭

ko.cleanNode($('#add-student')[0]);
ko.applyBindings(app.mainViewModel, $('#add-student')[0]);

但是我需要使用上面两行代码,因为它需要重置我的表单。

如果我们延迟清理节点,那么它工作正常。

function saysomething(){
			ko.cleanNode($('#add-student')[0]);
			ko.applyBindings(app.mainViewModel, $('#add-student')[0]);
     }
     setTimeout(saysomething, 500);	}

请帮我解决这个问题。我不想使用 setTimeout。请就此提出最佳做法。

谢谢

【问题讨论】:

    标签: javascript twitter-bootstrap user-interface knockout.js user-experience


    【解决方案1】:

    不用setTimeout,你可以监听hidden事件:

    $('#modal-id').on('hidden.bs.modal', function (e) {
      ko.cleanNode($('#add-student')[0]);
      ko.applyBindings(app.mainViewModel, $('#add-student')[0]);
    })
    

    Bootstrap's documentation on handling modal events

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 2012-05-11
      • 2015-01-30
      • 2014-04-15
      • 2017-11-18
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多