2017-05-23 4 views
0

여기 녹아웃 JS에서 새내기. 나는 아직도 것을 배웠다. 임 하나의 데이터 바인딩에 여러 이벤트를 바인딩하려고. 내가 어떻게 이걸 얻을 수 있니? 단일 이벤트에서 2 개의 함수를 실행해야합니다.녹아웃을 사용하여 데이터 바인딩에서 여러 이벤트를 바인딩하는 방법

HTML

당신은 다른 기능을 제공 할 수있는 1

function Browse(data) { 

     var data = new FormData(); 
     var files = $("#ImportFile").get(0).files; 
     if (files.length > 0) { 
      $("#tab1").removeClass("active"); 
      $("#tab2").addClass("active"); 

      $("#tabSelect").removeClass("active"); 
      $("#tabImport").addClass("active"); 

      fileName(files[0].name); 
      data.append("UploadedFile", files[0]); 


      self.changeMethod = function (data, event) { 
       self.file(event.currentTarget.files[0]); 
       self.fileSize(event.currentTarget.files[0].size); 
      } 

     } 

    } 

기능 # 2

self.fileChange=function(data){  
    if (window.File && window.FileReader && window.FileList && window.Blob) 
    { 
     //get the file size and file type from file input field 
     var fsize = data.size; 
     var ftype = data.type; 

     if(fsize>10) 
     { 
      alert(fsize +" bites\nToo big!"); 
      self.file("") 
     } 

     switch(ftype) 
     { 
      case 'image/png': 
      case 'image/gif': 
       break; 
      default: 
       alert('Unsupported File!'); 
       self.file("") 
     } 
    }else{ 
     alert("Please upgrade your browser, because your current browser lacks some new features we need!"); 
    } 
    }; 
+0

주입니다 : jQuery를 셀렉터가 넉 아웃에서 코드 냄새입니다. ViewModel을 변경하고 Knockout이 DOM을 관리하도록해야합니다. [css 바인딩] (http://knockoutjs.com/documentation/css-binding.html)을 참조하십시오. –

답변

1

기능 번호 새 함수에서 호출 할 2 개의 함수를 호출하십시오. 여기

샘플을

var viewModel = function() { 
 
    var self = this; 
 
    self.isFirstMethodCalled = ko.observable(false); 
 
    self.isSecondMethodCalled = ko.observable(false); 
 
    self.triggerFirstAndSecondMethods = function(data){ 
 
    self.firstMethod(data); 
 
    self.secondMethod(data); 
 
    }; 
 
    self.firstMethod = function(data){ 
 
    self.isFirstMethodCalled(true); 
 
    } 
 
    self.secondMethod = function(data){ 
 
    self.isSecondMethodCalled(true); 
 
    } 
 
}; 
 

 
ko.applyBindings(new viewModel());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> 
 

 
<input type="button" data-bind="click: triggerFirstAndSecondMethods" value="Call First And Second Method"/> 
 
<div data-bind="visible: isFirstMethodCalled"> 
 
    First Method has been called. 
 
</div> 
 
<div data-bind="visible: isSecondMethodCalled"> 
 
    SecondMethod has been called. 
 
</div>