KnockoutJS subscribe to multiple observables with same callback action

后端 未结 5 1051
遇见更好的自我
遇见更好的自我 2020-12-10 01:53

I\'ve got a model class in KnockoutJS which has multiple values that I\'d like to subscribe to. Each subscription will perform the same task, like so:

funct         


        
5条回答
  •  攒了一身酷
    2020-12-10 02:11

    Improving upon refactoring the function body into a variable, by turning the list of dependencies to track into a loop:

    function CaseAssignmentZipCode(zipCode, userId, isNew) {
      var self = this;
      self.zipCode = ko.observable(zipCode);
      self.userId = ko.observable(userId);
      self.isNew = isNew;
      self.isUpdated = false;
    
      var handler = function () { self.isUpdated = true; };
    
      ko.utils.arrayForEach([self.zipCode, self.userId], function(obs) {
        obs.subscribe(handler);
      });
     } 
    

提交回复
热议问题