King
King

Reputation: 53

want to use two click function in one binding in knockoutJs

Hi folks am new to knockoutJs,i want to use two click function in one binding.I tried like below but only first part is working .if i use attr: { href: '#/User/Add/' + user_id } after click then attr will work,select user is not working n vicevirse.Help me in this regard.Am Using Durandal as a roUter

 <a data-bind="click: $root.SelectedUser,attr: { href: '#/User/Add/' + user_id }"></a>

Upvotes: 0

Views: 72

Answers (2)

Erikas Pliauksta
Erikas Pliauksta

Reputation: 1532

I suggest to create one function within your viewmodel, which selects user and then redirects to your desired location:

define(['plugins/router'], function(router){
var userIds = ['1', '2', '3'] //this is just an example. use your own ids here
var SelectedUser = ko.observable('');

var buttonClick = function(user){
    SelectedUser(user);
    router.navigate('#/User/Add/' + user);
}

return {
    userIds: userIds,
    buttonClick: buttonClick
}}

in your html:

<ul data-bind="foreach: userIds">
<li>
    <a data-bind="click: $parent.buttonClick"></a>
</li>

Upvotes: 2

Captain John
Captain John

Reputation: 1891

Any reason you want to do this?

An alternative could be to call durandal routing in your click event.

var clickEvent = function(data, e) {
    doSelectedUserStuff();
    callDurandalRoute('#/User/Add/' + user_id);
}

Upvotes: 0

Related Questions