简体   繁体   English

如何在KnockOut.js中停止多重绑定

[英]How to stop the multiple binding in KnockOut.js

I have a view model like this : 我有一个这样的视图模型:

function ImageViewModel() 
{    
    var self = this;

    self.totalRecordCount = ko.observable();  

    self.currentRecordCount = ko.observable();

    self.images = ko.observableArray();
     // fetching all available images
    getAvailableImages(self, imageGalleryId, 1);//1 is page number for paging
}

I have html as followes: 我有如下的HTML:


<div id="available Images" class="available-images" data-bind="foreach:images">
<div c`enter code here`lass="available-image">
<div class="col-sm-4 thumbnail">
<asp:CheckBox ID="cbxImage" runat="server" CssClass="checkbox" />
<img alt="" data-bind="attr: { 'src': ImagePath, id: 'img_' + ImageId, 'data-id': ImageId }"
style="border: none;" />
</div>
</div>
</div>

i have java script at the page bottom as : 我在页面底部的Java脚本为:

  $('.galleryfooter').click(function () {
            $(this).attr('data-target', '#imageModal');
            $(this).attr('data-toggle', 'modal');
            ko.applyBindings(new ImageViewModel(), document.getElementById("imageGallery"));
        });

when i first Clicked the images are bind properly but when i clicked the button again then images are get multiplied.Means if i have 5 images in database it displays 25 images.So what should i do? 当我第一次单击图像时,图像被正确绑定,但是当我再次单击按钮时,图像被相乘。意味着如果我在数据库中有5张图像,它将显示25张图像。那我该怎么办?

Stop using jQuery to handle events. 停止使用jQuery处理事件。 Knockout has bindings for that. 淘汰赛对此具有约束力。 The agreement you have with Knockout is that it controls the DOM and you only manipulate the viewmodel. 您与Knockout达成的协议是,它可以控制DOM,并且您只能操作视图模型。

See the click binding and the attr binding . 请参见click绑定attr绑定 Also, if you have not gone through the Knockout tutorial , I highly recommend it. 另外,如果您还没有完成Knockout教程 ,则强烈建议您使用。 It will help you let go of the DOM. 它将帮助您放开DOM。

I agree with Roy. 我同意罗伊。 You need to separate your concerns better. 您需要更好地分离您的关注点。 That is the whole point of having a controller. 这就是拥有控制器的全部要点。 As it stands with every click you are reapplying the binding. 每次点击都可以重新绑定。 The binding should only be applied once. 绑定只能应用一次。 It should be more like this. 应该更像这样。

function ImageViewModel() 
{    
    var self = this;

    self.totalRecordCount = ko.observable();  

    self.currentRecordCount = ko.observable();

    self.images = ko.observableArray();
    // fetching all available images
    getAvailableImages(self, imageGalleryId, 1);//1 is page number for paging

    this.clickThis = function()
    {
        //doStuff
    };
}

ko.applyBindings(new ImageViewModel(), document.getElementById("imageGallery"));

I don't know where your gallery footer is supposed to go. 我不知道你的画廊页脚应该去哪里。 But here is a guess. 但这是一个猜测。

<div class="galleryfooter" data-bind="click: function(e){$root.clickThis();}"></div>

Also use knockout for attribute binds instead of jquery. 也可以使用敲除属性绑定而不是jquery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM