繁体   English   中英

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

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

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

我有这样的东西。

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

但是Botostrap的一流api意味着我们不需要执行任何JS方法,它们的小部件将自动运行。 问题是我怀疑在我的{{model}}被Ajax请求填充之前,Bootstrap会执行此操作。 因此,该轮播无法正常工作。

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

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

  1. 任何想法?
  2. 为什么存在-https: //github.com/emberjs-addons/ember-bootstrap 是否确实可以在这里解决我的问题? (尽管没有轮播)

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

因此,我对此有一个解决方案,但这不是为了保证质量。

对于Carousel,Bootstrap对其所需的元素还不够具体。 当轮播功能盘点要操作的元素时,它会阻塞Ember注入DOM的变形标签。 基本上,当要查看有多少个图像时,总会发现比实际多2个图像。

我对引导程序库中轮播的基础代码进行了更改,这就是我所做的。

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

这有助于轮播插件忽略metamorph标签。

希望这可以帮助。

我遇到了同样的问题,并使用以下方法解决了该问题。 请注意,我使用的是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

暂无
暂无

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

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