简体   繁体   English

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

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

I have simple controller 我有简单的控制器

@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

and view files: 并查看文件:

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

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

I would like to access a method on my place model instance called place.posts() , which returns the associated Collection of the model Post via Supermodel.js ( http://pathable.github.io/supermodel/ ). 我想访问我的place模型实例上的方法place.posts() ,该方法通过Supermodel.js( http://pathable.github.io/supermodel/ )返回模型Post的关联Collection I'm using Eco templates ( https://github.com/sstephenson/eco ) and have been following the patterns in the tutorials at http://www.backbonerails.com . 我正在使用Eco模板( https://github.com/sstephenson/eco ),并一直遵循http://www.backbonerails.com上的教程中的模式。

Anyone know how I should go about accessing the associated posts in my Eco template? 有人知道我应该如何访问我的Eco模板中的相关posts吗? Preferably I'd like to be able to access the actual method .posts() but I would settle for creating a posts variable in my view and passing that into the template. 最好是,我希望能够访问实际的方法.posts()但是我愿意在自己的视图中创建一个posts变量并将其传递到模板中。

Thanks and if you need any more info, please ask. 谢谢,如果您需要更多信息,请询问。

I usually do such thing in either of these two ways: 我通常通过以下两种方式之一来执行此操作:

  1. override serializeData() in Show.Place view, include posts() data in result json, then access the posts data in template by @posts . Show.Place视图中重写serializeData()Show.Place json中包含posts()数据,然后通过@posts访问模板中的posts数据。

  2. override templateHelpers() in the view, and access the posts data in template by @posts() . 在视图中重写templateHelpers() ,并通过@posts()访问template中的posts数据。

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

You can use a CompositeView from Marionette to extract your logic from the template, it can handle a model and a collection: 您可以使用Marionette的CompositeView从模板中提取逻辑,它可以处理模型和集合:

So you'll have a template for the CompositeView without loop logic, in this view you must define an itemView and an itemViewContainer which will contain occurrence(s) of itemView binded to the models from your collection. 因此,您将拥有一个无循环逻辑的CompositeView模板,在此视图中,您必须定义一个itemView和一个itemViewContainer ,其中将包含绑定到您模型中的模型的itemView出现。 After that you'll have to create the ItemView for your CompositeView with its own template. 之后,您必须使用自己的模板为CompositeView创建ItemView

I'm sure you'll find all the information you need with better explanations in the documentation ;) 我相信您会在文档中找到所需的所有信息以及更好的说明;)

Marionette.CompositeView Marionette.CompositeView

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

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