Backbone.Marionette - access to variables in the ItemView template or in the CompositeView template

Here I want to access a variable or a list of variables that are passed when initializing a new view from the corresponding template.

Code example

Creating a List View

@Taskit.module "Tasks.List", (List, Taskit, Backbone, Marionette, $, _) -> class List.NewTask extends Taskit.Views.ItemView template: JST["backbone/taskit/tasks/tasks/list/_templates/new_task"] 

Template for viewing the list above

 <div id="new-task-form"> </div> 

Initializing ItemView

 view = new Taskit.Tasks.List.NewTask project_id: "project_id" 

Here is my question: how can I access the variable "project_id" from my template.

 <%= project_id %> #is not working 

In Backbone, this can be achieved with

 $(@el).html(@template({task: @model, project_id: "project_id"})) 

How to do it in Marionette.js?

+6
source share
1 answer

You can provide your own method for serializing data:

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.itemview.md#itemview-serializedata

 Backbone.Marionette.ItemView.extend({ serializeData: function(){ var data = this.model.toJSON(); data.project_id = this.project_id; return data; } }); 
+12
source

Source: https://habr.com/ru/post/944327/


All Articles