Hi guys i asked a question yesterday about where to code dom manipulation / animations etc when using knockout. I had previously coded this into the viewmodel and was advised this is not correct.
Ive now updated my code and created custom binding handlers as advised, however i need to call a method of my viewmodel from within the custom binding handler. Specifically when the animation completes. Ive done it like this
ko.bindingHandlers.slideUp = {
init:function(element, valueAccessor, allBindingsAccessor, context){
var allBindings = allBindingsAccessor();
var parent = allBindings.parent;
$(element).click(function(){
if(parent.SelectedEmployee() === valueAccessor())return;
var $PanelWrapper = $('#' + allBindings.panelName);
var $Bottom = parseInt($PanelWrapper.css("bottom"));
if($Bottom === 0){
$PanelWrapper.animate({
bottom:"-95"
}, 500, function(){
parent.Select(valueAccessor());
}).animate({
bottom:"0"
}, 500);
}else{
parent.Select(valueAccessor());
$PanelWrapper.animate({
bottom:"0px"
}, 500);
}
});
}
}
and my binding is
<tbody data-bind="foreach: Emps">
<tr data-bind="slideUp: $data, parent: $parent, panelName: 'PanelWrapper'">
<td data-bind="text: Name"></td>
</tr>
</tbody>
As you can see im passing in a reference to the viewmodel and also the id of the tag i want to animate. Im just wondering if this is the correct way to do this. Is there a better way to reference the viewmodel, am i calling the method of the viewmodel correctly and is it ok to pass in the id of the html tag i want to use. It all works fine, but im keen to improve
heres a link to my fiddle