어떤 이유로 아래 코드는 링크의 클릭 이벤트를 두 번 발동합니다. 나는 비교적 녹아웃에 새로운이고 나는 나의 관례 바인딩을 잘못 할지도 모르다는 것을 생각한다. 아무도 내가 잘못한 것을 말해 줄 수 있습니까? (, BTW 나는 바이올린을 게시하지 않은 이유는 내가 jsfiddle에 GitHub의에서 매핑 플러그인을 포함 할 수 없다는 것입니다.)왜이 이벤트는 녹아웃에서 두 번 처리됩니까?
JS :
ko.bindingHandlers.activityContent = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called when the binding is first applied to an element
// Set up any initial state, event handlers, etc. here
var content = document.createElement("p");
content.innerHTML = '<a href="javascript:void(0)" data-bind="text: user_name, click: $parent.NavigatePage.bind($data, \'profile\', user_id)"></a>';
element.appendChild(content);
ko.applyBindings(bindingContext, content);
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called once when the binding is first applied to an element,
// and again whenever the associated observable changes value.
// Update the DOM element based on the supplied values here.
}
};
var activities = ko.mapping.fromJS({Activities: [{
"user_id": "52b5042d572b94ceadf6asdf1a2a5bc",
"user_name": "Sean Templeton"
}, {
"user_id": "52b5042d57asfda2b94ce61a2a5bc",
"user_name": "Sean Templeton"
}, {
"user_id": "52b5042d572b94ce61a2a5bc",
"user_name": "Sean Templeton"
}, {
"user_id": "52b5042d5asdfasdf72b94ce61a2a5bc",
"user_name": "Sean Templeton"
}, {
"user_id": "52basdf5042d572b94ce6asdf1a2a5bc",
"user_name": "Sean Templeton"
}], NavigatePage: function(page, userId) { console.log(this); console.log(page); console.log(userId()); }});
ko.applyBindings(activities);
HTML :
<Ul data-bind="foreach: Activities">
<li data-bind="activityContent: $data"></li>
</ul>
어떤 KO 버전을 사용하고 있습니까? 왜 핸들러에 html을 넣을까요? 왜 인라인이나 별도의 템플릿 (http://jsfiddle.net/eTXQJ/)을 사용하지 않으시겠습니까? – nemesv
innerHTML이 동적이기 때문에 Knockout v3.x – LordZardeck
KO 버전에 대한 확신이 있습니까? 코드에서 "메시지 : 동일한 요소에 바인딩을 여러 번 적용 할 수 없습니다." 오류 KO3.0 http://jsfiddle.net/hLpzB/를 사용하는 경우 2.2.1과 같은 꽤 오래된 KO 버전을 사용하는 경우에만이 두 번 클릭 동작을 repro 수 있습니다 ... – nemesv