0

我有一个画廊列表:

 <div class="gallery">
            <a href="#" class="next">next</a>
            <a href="#" class="prev">prev</a>

            <div class="frame">
                <ul data-bind="foreach: designs">
                    <li>
                        <span class="text">№&lt;span data-bind="text: getID()"></span> <span data-bind="text: getName()"></span></span>
                         <img src="images/img01.jpg" width="182" height="102" alt="image description"/>
                    </li>
                </ul>
            </div>

  </div>

JS

Models.Design = function () {...}

function ViewModel(){   
    this.designs = ko.observableArray();


    this.addDesign = function (_id, _name, _image, _video) {

        var design = new Models.Design;

        design.setID(_id);
        design.setName(_name);

        this.designs.push(design);
   };

   this.addDesign(1, "Image 1");
   this.addDesign(2, "Image 1");
   this.addDesign(3, "Image 1");
 // at this moment there is no elements in DOM
}

问题:我需要在哪里写$('.gallery').galleryScroll();来更新我的画廊滑块?

4

2 回答 2

3

只需订阅 observable。对集合的任何可见*更改都将调用您的回调。

function ViewModel() {
    this.designs = ko.observableArray();
    this.designs.subscribe(function () {
        $('.gallery').galleryScroll();
    });

    // ...
}

* 可见意味着你对 observable 执行操作,而不是直接对底层数组执行操作。

于 2013-02-03T21:29:16.820 回答
0

我找到了解决方案:

ko.bindingHandlers.gallery = {
    update: function(element, valueAccessor) {
        setTimeout(function() {
            $(element).parents('.gallery').galleryScroll();
        });
    }
};

 <ul data-bind="foreach: designs, gallery:{}">
     <li>....</li>
 </ul>
于 2013-02-03T19:42:42.710 回答