简体   繁体   中英

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/ ). I'm using Eco templates ( https://github.com/sstephenson/eco ) and have been following the patterns in the tutorials at http://www.backbonerails.com .

Anyone know how I should go about accessing the associated posts in my Eco template? 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.

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 .

  2. override templateHelpers() in the view, and access the posts data in template by @posts() .

Check https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.view.md for more info.

You can use a CompositeView from Marionette to extract your logic from the template, it can handle a model and a collection:

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. After that you'll have to create the ItemView for your CompositeView with its own template.

I'm sure you'll find all the information you need with better explanations in the documentation ;)

Marionette.CompositeView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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