【问题标题】:Ember.js - jQuery-masonry + infinite scrollEmber.js - jQuery-masonry + 无限滚动
【发布时间】:2013-08-27 15:29:57
【问题描述】:

我正在尝试在我的 ember 项目中进行无限滚动和砌体工作。砌体“砖”是带有文字和图像的帖子。目前我可以让第一页显示在页面的初始加载并应用砌体(但我仍然需要做一个 setTimeout,试图弄清楚如何摆脱它)。

我也有基本的无限滚动代码。现在我只是从后端抓取第二页。当滚动到页面底部时,第二页的帖子被加载。我想弄清楚的是如何在插入时将砌体应用到新页面。

$('#masonry-container').masonry('refresh') 放入didInsertElement 不起作用,因为视图元素已经呈现,我只是将新元素附加到现有视图。

有没有可以让我刷新的事件?

下面的代码在初始加载时应用砌体,然后在滚动到底部时加载第二页,但当前不刷新砌体。

谢谢

路由:

App.Router.map ->
  @route 'posts', path: '/'

App.PostsRoute = Ember.Route.extend
  model: -> 
    App.Post.find()

    events:
      more: ->
        App.Post.query(page: 2) # hard code for now, next page will come from back end

查看:

App.PostsView = Ember.View.extend
layoutName: 'appLayout'
templateName: 'posts'
controller: 'posts'

didInsertElement: ->
  @scheduleMasony()
  $(window).bind('scroll', =>
    @.didScroll()
  )

scheduleMasony: -> 
  Ember.run.schedule('afterRender', @, @applyMasonry)

applyMasonry: ->
  setTimeout(-> 
    $('#masonry-container').masonry(
      itemSelector: '.box',
      columnWidth: 220,
      gutterWidth: 10,
      isFitWidth: true,
      isAnimated: true
    )
  2000)

didScroll: -> 
  if @isScrolledToBottom()
    @get('controller').send('more')

isScrolledToBottom: -> 
  distanceToTop = $(document).height() - $(window).height()
  top = $(document).scrollTop()
  top == distanceToTop

模板:

app_layout.handlebars

...layout mark up...
<div id="container">
  {{yield}}
</div>
...more layout mark up...

posts.handlebars:

<div id="masonry-container" class="transitions-enabed infinite-scroll clearfix centered">
{{#each controller.content}}
  <div class="col2 box">
    <h3>{{title}}
    <img {{bindAttr src="imageUrl"}} >
    <p class="text">
      {{text}}
    </p>
  </div>
{{/each}}
</div>

application.handlebars

{{outlet}}

【问题讨论】:

  • 刚刚编辑了我的答案,我拼错了length 抱歉...
  • 我的意思是 lengthcontroller.content.length 中是 lenght,因此无法正常工作,我希望它现在可以正常工作...

标签: javascript ember.js jquery-masonry infinite-scroll


【解决方案1】:

当滚动到页面底部时,第二页的帖子被加载。我想弄清楚的是如何在插入时将砌体应用到新页面。

我猜你最好观察控制器的 content 长度以重新调用砌体,因为附加到 DOM 与你显示的帖子数量有关。

所以试试这样的:

App.PostsView = Ember.View.extend({
  layoutName: 'appLayout'
  templateName: 'posts'
  controller: 'posts'
  ...
  contentDidChange: function() {
    // check for 'inDOM' just to be sure
    // we don't run this unnecessarily 
    // when the view is not yet rendered
    if(this.get('state') === "inDOM") {
      applyMasonry();
    }
  }.observes('controller.content.length')
  ...
});

希望对你有帮助。

【讨论】:

  • 谢谢,将观察者放在 content.length 上并检查 inDOM 是否有效。我所做的唯一修改是为砌体应用/刷新功能添加去抖动功能,这样它就不会因每页加载的每条内容而关闭。
  • @Bryan 你能用你的解决方案更新你的问题吗?我做了类似的事情,但仍然没有工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 2012-08-08
  • 2017-10-09
相关资源
最近更新 更多