简体   繁体   English

bxSlider无法与Knockoutjs一起正常工作

[英]bxSlider not working properly with Knockoutjs

I'm trying to get bxSlider to work properly after the data is loaded from db. 我试图从数据库加载数据后使bxSlider正常工作。

My knockoutjs code works as expected: 我的基因敲除代码按预期工作:

BspApp.UspjesnaPrica = function (n) {
    var self = this;

    self.nid = n.id;
    self.naslov = n.naslov;
    self.datum = n.datum;
    self.prica = n.prica;
    self.detailUrl = '/vise/prica/' + n.id;
    self.imgUrl = n.imgPath;
}

BspApp.UspjesnePrice = function () {
    var self = this;
    self.price = ko.observableArray([]);
    self.a = "parent";

    self.ucitajSve = function () {

        $.get('dal/servis.php', { tip: 'price' }, function (price) {
            $.each(price, function (indx, prica) {
                self.price.push(new BspApp.UspjesnaPrica(prica));
            });
        }, 'json');
    }
}

And this is my html: 这是我的html:

    <ul data-bind="foreach: price" id="priceSlider">
        <li>
            <a data-bind="attr: { href: detailUrl }">
                <img data-bind="attr: { src: imgUrl, title: naslov }" />
            </a>
        </li>
    </ul>

This is bxSlider and KO init code: 这是bxSlider和KO的初始化代码:

var uspjesneprice = new BspApp.UspjesnePrice();
uspjesneprice.ucitajSve();
ko.applyBindings(uspjesneprice, document.getElementById('uspjesneprice'));

var priceSlider = $("#priceSlider").bxSlider({
    mode: 'horizontal',
    hideControlOnEnd: true,
    adaptiveHeight: true,
    captions: true
});

After my data is loaded bxSlider not displaying correctly. 加载数据后,bxSlider无法正确显示。

Also, hard-coded html tags works without problem. 同样,硬编码的html标签也可以正常工作。

Because you're probably initializing the bxSlider before the ajax-request to dal/servis.php has finished. 因为您可能bxSlider在对dal/servis.php的ajax请求完成之前初始化bxSlider

What if you move the bxSlider code inside the ajax-callback: 如果在ajax回调中移动bxSlider代码怎么办:

$.get('dal/servis.php', { tip: 'price' }, function (price) {
    $.each(price, function (indx, prica) {
        self.price.push(new BspApp.UspjesnaPrica(prica));
    });
    var priceSlider = $("#priceSlider").bxSlider({
        mode: 'horizontal',
        hideControlOnEnd: true,
        adaptiveHeight: true,
        captions: true
    });
}, 'json');

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

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