簡體   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