1

我将 MVC3 与 Knockout.js 一起使用,并希望在模板中使用 data-bind=addContribute 将一些数据从 api 附加到我的按钮。此按钮应打开一个弹出框,我需要该弹出框上的附加按钮数据。我怎样才能做到这一点?

我的模板:

<div>
        <ul data-bind="template: {name: 'membersTemplate', foreach: viewModel.membersList}">
        </ul>
</div>

<script id="membersTemplate" type="text/html">
    <li>
        <div class="fl width165">
            <img src=" ${ image } " width="33" height="34" />
            <span> ${ memberName } </span>
            ${ memberType }
        </div>
        <aside class="fr margint10">
            <label> ${ contributions } </label>
            <a href="#" class="sprite-add_img" id="openContribute" title="Add Contributes to Goals" data-bind="click: viewModel.addContribute" ></a>
        </aside>
    </li>
</script>
4

1 回答 1

1
<script id="membersTemplate" type="text/html">
    <li>
        <div class="fl width165">
            <img data-bind="attr : {src : img}" width="33" height="34" />
            <span data-bind="text : memberName"></span>
            <span data-bind="text : memberType"></span>
        </div>
        <aside class="fr margint10">
            <label data-bind="text : contributions"></label>
            <a href="#" class="sprite-add_img" id="openContribute" title="Add Contributes to Goals" data-bind="click: addContribute" ></a>
        </aside>
    </li>
</script>  

membersList你代码中的变量应该是下一个

function SingleMember(img, name, type, contr)
{
   var self = this
   self.img = ko.observable(img)
   self.memberName = ko.observable(name)
   self.memberType = ko.observable(type)
   self.contributions = ko.observable(contr)  
   self.addContribute = function() {  
       //  
    }
 }

window.viewModel = new function()  
{  
   var self = this  
   self.membersList = ko.observableArray()  
   self.membersList.push(new SingleMember(/*.... params1*/))  
   self.membersList.push(new SingleMember(/*.... params2*/))
}
于 2012-11-01T20:24:42.247 回答