无法让Bootstrap轮播与Ember合作

时间:2013-08-05 22:40:03

标签: javascript twitter-bootstrap ember.js carousel

试图理解为什么它不起作用。

我有类似的东西。

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

但是Botostrap的第一个类api意味着我们不需要执行任何JS方法,它们的小部件将自动运行。问题是我怀疑Bootstrap会在我的{{model}}被Ajax请求填满之前执行此操作。所以这个Carousel将不起作用。

令人讨厌的是我已经尝试关闭他们的数据-api - $(document).off('。data-api');并手动调用他们的轮播方法 - 仍然无法正常工作。

轮播使用硬编码数据 - 但是一旦我尝试从我的Ember模型填充轮播div项目,它就会停止工作。

  1. 任何想法?
  2. 为什么会这样 - https://github.com/emberjs-addons/ember-bootstrap?它是否存在以完全解决我的问题? (虽然没有旋转木马)

3 个答案:

答案 0 :(得分:9)

1 - 我希望this 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 - 我不知道为什么旋转木马不包含在ember-boostrap中。

答案 1 :(得分:4)

所以我有一个解决方案,但这不是为了娇气。

Bootstrap在Carousel的情况下对于它所寻找的元素不够具体。当轮播功能去盘点它要操纵的元素时,它会扼杀Ember注入DOM的变形标签。基本上,当它看到有多少图像时,总会找到比实际图像多2的图像。

我在bootstrap库中对carousel的底层代码进行了更改,这就是我所做的。

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()])

这有助于轮播插件忽略变形标记。

希望这有帮助。

答案 2 :(得分:3)

我遇到了同样的问题,并使用以下方法解决了这个问题。请注意,我使用的是ember-cli,但它很容易适应。

这是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>

这是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();
    }
});

请注意,未来的Ember版本不需要手动设置active类,因为each帮助程序将提供当前项目的index