KnockoutJS subscribe to multiple observables with same callback action

后端 未结 5 1093
遇见更好的自我
遇见更好的自我 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:30

    Do you not want to duplicate handler function's body? Extract it to a variable.

    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; };
    
      self.zipCode.subscribe(handler);
      self.userId.subscribe(handler);
    }
    

提交回复
热议问题