2017-04-22 118 views
1

我正在使用淘汰賽對於某些項目,ASP.NET MVC
我使用淘汰賽什麼是Knockout自定義綁定「之後」變量?

ko.bindingHandlers.select2 = { 
    after: ["options", "value", "selectedOptions"], 
    init: function (el, valueAccessor, allBindingsAccessor, viewModel) { 
     // no explicit reference to the 'after' variable 
    }, 
    update: function (el, valueAccessor, allBindingsAccessor, viewModel) { 
     // no explicit reference to the 'after' variable 
    } 
} 

以下bindingHandler我從this question得到這個代碼,我修改了它的小。
對於Select2 plugin,它基本上是custom binding handler

問題
我只是想知道什麼after: ["options", "value", "selectedOptions"],在這裏的意思。在initupdate函數中沒有任何地方提及這個變量。
這個變量在這方面有什麼含義嗎?或者這是一個淘汰的指令,使其在完成執行[options,value,selectedOptions]綁定後執行此自定義綁定?

注意 custom binding的文檔沒有提到這個變量。

回答

1

你是對的,它似乎沒有證件。挖掘到KO source code告訴我們這個:

// First add dependencies (if any) of the current binding 
if (binding['after']) { 
    cyclicDependencyStack.push(bindingKey); 
    ko.utils.arrayForEach(binding['after'], function(bindingDependencyKey) { 
     if (bindings[bindingDependencyKey]) { 
      if (ko.utils.arrayIndexOf(cyclicDependencyStack, bindingDependencyKey) !== -1) { 
       throw Error("Cannot combine the following bindings, because they have a cyclic dependency: " + cyclicDependencyStack.join(", ")); 
      } else { 
       pushBinding(bindingDependencyKey); 
      } 
     } 
    }); 
    cyclicDependencyStack.length--; 
} 

你的假設看起來是正確的。 KO正在構建一個依賴綁定的列表,它必須在當前綁定運行之前運行。內置的valueselectedOptions綁定利用這個關鍵字。

這裏是discussion on implementation from the Knockout Github

下面是相關StackOverflow answer

見的jsfiddle在答案示例代碼。

相關問題