简体   繁体   English

无法让Bootstrap轮播与Ember一起使用

[英]couldn't get Bootstrap carousel to work with Ember

trying to understand why its not working. 试图了解为什么它不起作用。

I have something like this. 我有这样的东西。

<div class="carousel slide" id="new-prospect-container">
   <div class="carousel-inner">
   {{#each model}}
      <div class="item">
      ...
     </div>
   {{/each}}
   </div>
</div>

But Botostrap's first class api means that we don't need to execute any JS methods and their widgets will work automatically. 但是Botostrap的一流api意味着我们不需要执行任何JS方法,它们的小部件将自动运行。 The problem is I suspect Bootstrap would have executed this prior to my {{model}} being filled up by an Ajax requests. 问题是我怀疑在我的{{model}}被Ajax请求填充之前,Bootstrap会执行此操作。 So this Carousel won't work. 因此,该轮播无法正常工作。

What's annoying is i already tried turning off their data-api - $(document).off('.data-api'); 令人讨厌的是,我已经尝试关闭其data-api-$(document).off('。data-api'); and manually call their carousel method - still won't work. 并手动调用其轮播方法-仍然无法正常工作。

The carousel works with hard coded data - but once I try to populate the carousel div items from my Ember model, it just stops working. 该轮播使用硬编码数据-但是一旦我尝试从我的Ember模型中填充轮播div项目,它就会停止工作。

  1. Any idea? 任何想法?
  2. Why does this exist - https://github.com/emberjs-addons/ember-bootstrap ? 为什么存在-https: //github.com/emberjs-addons/ember-bootstrap does it exist to exactly solve my issue here? 是否确实可以在这里解决我的问题? (although there's no carousel) (尽管没有轮播)

1 - I hope that this jsfiddle solve your problem. 1-我希望这个jsfiddle解决您的问题。

App.CarouselView = Ember.View.extend({    
    templateName: 'carousel',
    classNames: ['carousel', 'slide'],
    init: function() { 
        this._super.apply(this, arguments);
        // disable the data api from boostrap
        $(document).off('.data-api');      
        // at least one item must have the active class, so we set the first here, and the class will be added by class binding
        var obj = this.get('content.firstObject');
        Ember.set(obj, 'isActive', true);
    },
    previousSlide: function() {
        this.$().carousel('prev');
    },
    nextSlide: function() {
        this.$().carousel('next');
    },
    didInsertElement: function() {
        this.$().carousel();
    },
    indicatorsView: Ember.CollectionView.extend({
        tagName: 'ol',
        classNames: ['carousel-indicators'],        
        contentBinding: 'parentView.content',
        itemViewClass: Ember.View.extend({
            click: function() {
                var $elem = this.get("parentView.parentView").$();
                $elem.carousel(this.get("contentIndex"));
            },
            template: Ember.Handlebars.compile(''),
            classNameBindings: ['content.isActive:active']            
        })
    }),
    itemsView: Ember.CollectionView.extend({        
        classNames: ['carousel-inner'],
        contentBinding: 'parentView.content',
        itemViewClass: Ember.View.extend({
            classNames: ['item'],
            classNameBindings: ['content.isActive:active'],
            template: Ember.Handlebars.compile('\
                <img {{bindAttr src="view.content.image"}} alt=""/>\
                <div class="carousel-caption">\
                    <h4>{{view.content.title}}</h4>\
                    <p>{{view.content.content}}</p>\
                </div>')
        })
    })
});

2 - I don't know why the carousel isn't include in ember-boostrap. 2-我不知道为什么旋转木马不包含在ember-boostrap中。

So I have a solution for this, but it's not for the squeamish. 因此,我对此有一个解决方案,但这不是为了保证质量。

Bootstrap isn't specific enough about what elements it looks for in the case of the Carousel. 对于Carousel,Bootstrap对其所需的元素还不够具体。 When the carousel function goes to inventory what elements it's to manipulate, it chokes on the metamorph tags that Ember injects into the DOM. 当轮播功能盘点要操作的元素时,它会阻塞Ember注入DOM的变形标签。 Basically, when it goes to see how many images there are, it will always find 2 more than there actually are. 基本上,当要查看有多少个图像时,总会发现比实际多2个图像。

I made changes to the underlying code of the carousel in the bootstrap library, here's what I did. 我对引导程序库中轮播的基础代码进行了更改,这就是我所做的。

Line 337, change:
this.$items  = this.$active.parent().children()
TO
this.$items  = this.$active.parent().children('.item')


Line 379, change:
var $next     = next || $active[type]()
TO
var $next     = next || $active[type]('.item')


Line 401, change:
var $nextIndicator = $(that.$indicators.children()[that.getActiveIndex()])
TO
var $nextIndicator = $(that.$indicators.children('li')[that.getActiveIndex()])

This helps the carousel plugin ignore the metamorph tags. 这有助于轮播插件忽略metamorph标签。

Hope this helps. 希望这可以帮助。

I had the same issue and solved it by using the following method. 我遇到了同样的问题,并使用以下方法解决了该问题。 Note that I'm using ember-cli but it's fairly easy to adapt. 请注意,我使用的是ember-cli,但适应起来很容易。

This is the templates/components/photo-carousel.hbs file: 这是templates/components/photo-carousel.hbs文件:

<div id="my-carousel" class="carousel slide" data-ride="carousel">

    <ol class="carousel-indicators">
        {{#each photo in photos}}
            <li data-target="#my-carousel" data-slide-to=""></li>
        {{/each}}
    </ol>

    <div class="carousel-inner" role="listbox">
        {{#each photo in photos}}
            <div class="item">
                <img {{bind-attr src="photo.completeUrl" title="photo.caption" alt="photo.caption"}} />
                <div class="carousel-caption">
                    {{photo.caption}}
                </div>
            </div>
        {{/each}}
    </div>

    <!-- removed right and left controls for clarity -->
</div>

This is the components/photo-carousel.js : 这是components/photo-carousel.js

export default Ember.Component.extend({
    didInsertElement: function () {

        // Add the active classes (required by the carousel to work)
        Ember.$('.carousel-inner div.item').first().addClass('active');
        Ember.$('.carousel-indicators li').first().addClass('active');

        // Set the values of data-slide-to attributes
        Ember.$('.carousel-indicators li').each(function (index, li) {
            Ember.$(li).attr('data-slide-to', index);
        });

        // Start the carousel
        Ember.$('.carousel').carousel();
    }
});

Note that setting the active classes manually will not be required with future versions of Ember since the each helper will provide the index of the current item. 请注意,在以后的Ember版本中,不需要手动设置active类,因为each帮助程序都将提供当前项目的index

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

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