繁体   English   中英

使用Marionette.js访问Eco模板中的实例方法

[英]Accessing instance methods in Eco templates using Marionette.js

我有简单的控制器

@Blobber.module "PlacesApp.Show", (Show, App, Backbone, Marionette, $, _) ->

  Show.Controller =

    showPlace: (place) ->
      showView = @getShowView place

      App.mainRegion.show showView

    getShowView: (place) ->
      new Show.Place
        model: place

并查看文件:

@Blobber.module "PlacesApp.Show", (Show, App, Backbone, Marionette, $, _) ->

  class Show.Place extends App.Views.ItemView
    template: "places/show/templates/_place"

我想访问我的place模型实例上的方法place.posts() ,该方法通过Supermodel.js( http://pathable.github.io/supermodel/ )返回模型Post的关联Collection 我正在使用Eco模板( https://github.com/sstephenson/eco ),并一直遵循http://www.backbonerails.com上的教程中的模式。

有人知道我应该如何访问我的Eco模板中的相关posts吗? 最好是,我希望能够访问实际的方法.posts()但是我愿意在自己的视图中创建一个posts变量并将其传递到模板中。

谢谢,如果您需要更多信息,请询问。

我通常通过以下两种方式之一来执行此操作:

  1. Show.Place视图中重写serializeData()Show.Place json中包含posts()数据,然后通过@posts访问模板中的posts数据。

  2. 在视图中重写templateHelpers() ,并通过@posts()访问template中的posts数据。

检查https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.view.md了解更多信息。

您可以使用Marionette的CompositeView从模板中提取逻辑,它可以处理模型和集合:

因此,您将拥有一个无循环逻辑的CompositeView模板,在此视图中,您必须定义一个itemView和一个itemViewContainer ,其中将包含绑定到您模型中的模型的itemView出现。 之后,您必须使用自己的模板为CompositeView创建ItemView

我相信您会在文档中找到所需的所有信息以及更好的说明;)

Marionette.CompositeView

暂无
暂无

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

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