【问题标题】:Different styling on the third record in Backbone + Marionette + RailsBackbone + Marionette + Rails 中第三张唱片的不同样式
【发布时间】:2013-07-08 18:07:24
【问题描述】:

我决定进入 Backbone 竞技场,在搜索了 BB 和 Marionette 的文档后,我在看似简单的事情上遇到了一些麻烦。

我只是想自定义第三条记录上显示的内容。

这就是我将如何使用 Rails 来做到这一点

    <% @posts.each_with_index do |post, i| %>
      <% if i == 1 || i == 7 || i == 14 %><!-- 1st, 7th, & 14th record -->
        display title, description & something else
      <% else %><!-- other records -->
        display only title
      <% end %>
    <% end %>

使用 Backbone + Marionette + Underscore,我的记录显示如下: 控制器

        postsRegion: (posts) ->
            postsView = @getPostsView posts

            postsView.on "childview:posts:post:clicked", (child, post) ->
                App.vent.trigger "posts:post:clicked", post

            @layout.postsRegion.show postsView

        getPostsView: (posts) ->
            new List.Posts
                collection: posts

查看

   class List.Post extends App.Views.ItemView
        template: "posts/list/_post"
        tagName: "li"
        className: "span4 item"

        events:
            "click" : -> @trigger "posts:post:clicked", @model

如何使第 1、7 和 14 条(或仅第三条)记录显示不同的内容?另外,作为一名设计师,任何人都可以建议任何关于视图的进一步阅读使用这个 js 库?

【问题讨论】:

    标签: ruby-on-rails backbone.js underscore.js marionette backbone-views


    【解决方案1】:

    在您的集合视图中,您应该能够根据索引将选项传递给项目视图,如下所示:

    MyCollectionView = Backbone.Marionette.CollectionView.extend({
      itemView : MyItemView,
      itemViewOptions : function (model, index) {
        if (index % 3 == 0) {
          return { specialValue : "foo" };
        } else {
          return {};
        }
      }
    };
    MyItemView = Backbone.Marionette.ItemView.extend({
      onRender : function () {
        if (this.options.specialValue) {
          // DO SOMETHING SPECIAL
        }
      }
    };
    

    Marionette 文档实际上非常棒。有问题的函数可以是found here

    【讨论】:

    • 好答案!不知道itemViewOptions 可以带索引。
    【解决方案2】:

    您可能会遇到此类行为的问题:Marionette 集合视图为集合中的每个模型呈现一个项目视图。反过来,这些项目视图仅根据模型中的数据调整显示的内容(即渲染的模板)。

    如果您想每隔 7 项突出显示一次,我会看到以下选项:

    总而言之,您将面临一场艰苦的战斗,如果您可以将模型属性用于造型目的,那么使用 Marionette 后您的生活会轻松很多。请注意,此模型属性不必保留在服务器上...

    【讨论】:

    • 我需要的第 7 项内容将是我的帖子缩略图的某个版本(我目前的缩略图是“big_image”和“thumb_image”)。我还需要另一个元素/记录来代替每 10 个项目。您可以在 Rails 中通过多种方式做到这一点。 使用 Marionette 是否不可行?我对涉及 Backbone 的任何其他建议或库持开放态度。 谢谢@David
    • 这里有很棒的解决方案!
    猜你喜欢
    • 2015-01-11
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多