2

I have code like this

var databaseViewModel = new function () {
    var self = this;
    self.databaseList = ko.observable("");
    self.id = ko.observable("");
    self.databaseSelected = ko.observable("");
    self.description = ko.observable("description");

    self.openList = function () {
        alert("openList");
    };
}

html:

<div data-bind="foreach: databaseList">
                <li><a href="javascript:void(0)" data-bind="click:'databaseViewModel.openList(this)',  attr: {   title: DataBaseName }">
                    <span class="pic1"></span><span data-bind="text:DataBaseName"></span></a></li>
                <div data-bind="attr: { 'data-something': DataBaseName }">
                    ...</div>
            </div>

I want to invoke openlist when I click the link DataBaseName,how to modify the code?

1 Answer 1

4

You would want to do data-bind="click: $parent.openList"

Check out these docs for more info on $parent and other special variables that you can use: http://knockoutjs.com/documentation/binding-context.html

1
  • Maybe reproduce your situation in jsFiddle. Here is one to start with: jsfiddle.net/rniemeyer/shv8F You would want your databaseList to be an observableArray. Jan 7, 2013 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.