简体   繁体   English

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

[英]Knockout Component View Not Updating When Its ViewModel Observable Changes

I have a component setup to use AMD to get the html template and viewmodel code. 我有一个组件设置,可使用AMD获取html模板和viewmodel代码。 Everything works fine. 一切正常。 The component loads when it is supposed to and behaves fine with the params passed to it. 组件应按预期加载,并且在传递给它的参数下表现良好。 The problem is I defined an observable in the viewModel whose value shows up in the template view, but when the observable's value changes the text on the view does NOT change. 问题是我在viewModel中定义了一个可观察对象,其值显示在模板视图中,但是当可观察对象的值更改时,视图上的文本不会更改。 Can anyone explain what is going on here? 谁能解释这是怎么回事? The text I am trying to bind to is modalTitle. 我要绑定的文本是modalTitle。 When the modal loads its title is 'TEMP' but if I go to the console and type 'window.modalTitle()' I get 'CREATE REPORT SCHEDULE'. 当模态加载时,其标题为“ TEMP”,但是如果我进入控制台并键入“ window.modalTitle()”,则会得到“ CREATE REPORT SCHEDULE”。 It's like the view is getting the first value of the observable and then ignoring it after that. 就像视图正在获取可观察到的第一个值,然后在此之后将其忽略。 Is there anyway I can force it to look for updates? 无论如何,我可以强迫它寻找更新吗?

ViewModel: (schedules.component.js) ViewModel:(schedules.component.js)

  define(['knockout'], function (ko) {
  console.log('schedules hit');
  loadCss('schedules');

  function SchedulesViewModel(params) {
    this.scheduledItems = params.scheduledItems;
    this.itemName = params.itemName;
    this.modalTitle = ko.observable("TEMP");
    window.modalTitle = this.modalTitle;
  }

  SchedulesViewModel.prototype.initiateAddScheduledItem = function () {
    this.modalTitle("CREATE " + this.itemName + " SCHEDULE");
    $('#schedulesModal').modal('show');
  };

  SchedulesViewModel.prototype.removeSelectedScheduledItem = function () {
    this.chosenValue('dislike');
  };

  window.ReportsApp.SchedulesViewModel = SchedulesViewModel;

  return SchedulesViewModel;
});

View Template 查看模板

<div id="schedulesModal" class="modal fade lcmsModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>-->
        <img src="/Content/images/modalASLogo.png" style="float: right;" />
        <h4 class="modal-title" data-bind="text: modalTitle()">Test Title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body ...</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">CANCEL</button>
        <button type="button" class="btn btn-primary">SAVE</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- /Bootstrap Modal -->

It does not get changed because this.itemName has not been defined as an observable. 它不会更改,因为this.itemName尚未定义为可观察的。 it is better to define a computed observable which will automatically update whenever any observables change. 最好定义一个可计算的可观测值,该值将在任何可观测值更改时自动更新。
Instead of using prototype to add methods, you can use knockout function which foes it for you. 除了使用prototype添加方法外,您还可以使用敲除功能为您提供帮助。 Example : https://jsfiddle.net/kyr6w2x3/34/ 范例: https : //jsfiddle.net/kyr6w2x3/34/

 function SchedulesViewModel(params) {
    var self = this ;
    self.scheduledItems = ko.observable(params.scheduledItems);
    self.itemName = ko.observable(params.itemName);
    self.modalTitle = ko.observable("TEMP");
    self.chosenValue= ko.observable();

   self.modalTitle = ko.computed(function() {
        return "CREATE " + self.itemName() + " SCHEDULE" ;
    }, self);

    // you can change below to show your modal whenever you want
    $('#schedulesModal').modal('show');

    self.removeSelectedScheduledItem = function (){
        self.chosenValue('dislike');
    }
  }


ko.applyBindings(new SchedulesViewModel({scheduledItems:"scheduledItems" ,itemName : "itemName" }));

Update : yes you can have multiple view models or better to say nested view models. 更新 :是的,您可以有多个视图模型,或者说嵌套视图模型更好。 Look at the new example and see how you can communicate between your models. 查看新示例,看看如何在模型之间进行通信。 https://jsfiddle.net/kyr6w2x3/35/ https://jsfiddle.net/kyr6w2x3/35/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM