【问题标题】:Can't get iscroll4 to play with backbone view无法让 iscroll4 使用主干视图
【发布时间】:2012-03-12 20:54:11
【问题描述】:

我正在尝试在一个主干.js 应用程序中使用 iScroll4。我有几个动态加载的列表,我想在加载适当的视图后初始化 iScroll。

我正在尝试在列表视图完成加载时调用“new iScroll”,但我终生无法弄清楚如何执行此操作。

有没有人让这两个一起工作?是否有一个主干视图在其元素加载后初始化滚动条的示例?

【问题讨论】:

  • 不能在加载内容之前在视图上初始化iScroll吗?如果我没记错的话,我相信 iScroll 是附加到父元素的,所以添加的任何数据都会滚动。
  • 当我这样做时,我得到了这个奇怪的“橡皮筋”效果,列表移动了一点,但不会滚动。我相信它需要先加载,以便库知道元素的高度。要么,要么我没有在视图中正确初始化 iScroll。我找不到如何做到这一点的在线示例。
  • 我明白了。阅读 iScroll 文档后,似乎宽度和高度都需要在初始化时设置。如果你问我,这会让整个插件变得不那么有用。 Sander 的解决方案似乎是唯一的方法,(在加载视图时设置回调,然后初始化 iScroll)。

标签: backbone.js iscroll4


【解决方案1】:

你是对的,你必须先加载视图, 或者之后刷新iscroll

在我们的应用程序中,我们通常使用render方法来渲染视图 并有一个 postRender 方法来处理这些额外插件的初始化,如 iscroll

当然,您需要一些手动工作来完成它,但这是它的要点:

var myView = Backbone.View.extend({

    // more functions go here, like initialize and stuff... but I left them out because only render & postRender are important for this topic

    // lets say we have a render method like this:
    render: function() {            
        var data = this.collection.toJSON();
        this.$el.html(Handlebars.templates['spotlightCarousel.tmpl'](data));
        return this;
    },

    // we added the postRender ourself:
    postRender: function() {            
        var noOfSlides = this.collection.size(); 
        $('#carouselscroller').width(noOfSlides * 320);

        this.scroller = new IScroll('carouselwrapper', {
            snap: true,
            momentum: false,
            hScrollbar: false
        });
    }

});

现在调用这些方法 我们在我们的视图之外做了这个,因为我们喜欢一些视图管理器来处理这个 但归根结底就是这个

    var col = new myCollection();
    var view = new myView({ collection: col });

    $('#wrapper').html(view.render().$el); // this chaining is only possible due to the render function returning the whole view again.

    // here we always test if the view has a postRender function... if so, we call it
    if (view.postRender) {
        view.postRender();
    }

【讨论】:

  • 我无法在我的应用程序上加载 iscroll。我正在使用主干、下划线和需要 js。我有这个代码。 var scroller = new iScroll('itemcontainer');它说 iScroll 不是构造函数。有什么想法吗?
猜你喜欢
  • 2013-03-07
  • 2015-01-31
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
相关资源
最近更新 更多