Is there a way to change Marionette ItemView template dynamically with RequireJS?
Asked Answered
W

3

7

I'm trying to manipulate itemViews dynamically in a Marionette CollectionView. The collections have the same models, but i defined templateName argument inside the models.

The question is, can i manipulate the ItemView template by this argument?

ItemView:

define(['text!templates/ComponentItemViewTemplate.html','models/ComponentModel'], function (template, model) {
    var ItemView = Backbone.Marionette.ItemView.extend({
        template: _.template(template),
        model: model
    });

    return ItemView;
});

CollectionView:

define(['views/ComponentItemView', 'views/LoadingView'], function(ItemView, LoadingView) {
    var ComponentListView = Backbone.Marionette.CollectionView.extend({
        emptyView : LoadingView,
        id: "component-list",
        itemView: ItemView, 
        events: {
            'click .title span' : 'show' 
        },
        appendHtml: function(collectionView, itemView, index){//i would like to render different templates, for different models.
            itemView.$el.draggable({ helper: "clone", cancel: ".component .title span", connectToSortable: ".ui-sortable" });
            collectionView.$el.append(itemView.el);
        },
        show: function(r) {
            var target = $(r.target);
            if( target.parent().hasClass('open') ){
                target.parent().removeClass('open');
                target.parent().next().slideDown('fast');
            }else{
                target.parent().addClass('open');
                target.parent().next().slideUp('fast');
            }
        }
    });

    return ComponentListView;
});

Thanks!

Welby answered 20/3, 2013 at 22:37 Comment(2)
I would assign the template name in the ItemView, with the getTemplate function. github.com/marionettejs/backbone.marionette/blob/master/docs/…Lipski
And is there a way, to render itemview after requirejs loaded the template? Cose with your sollution i get notemplate error, before the template is required. getTemplate: function() {require(['text!templates/' + this.model.get('editor_template') + '.html'], function(Template){return _.template(Template);});}Welby
E
25

You can override getTemplate function and write your custom logic there. The Marionette documentation recommends the following option:

MyView = Backbone.Marionette.ItemView.extend({
  getTemplate: function(){
    if (this.model.get("foo")){
      return "#some-template";
    } else {
      return "#a-different-template";
    }
  }
});
Eggbeater answered 2/7, 2013 at 9:11 Comment(0)
P
6

I think gumballhead is on the right track. You can override the getTemplate function to do this.


MyCollectionView = Marionette.CollectionView.extend({

  // ...

  getItemView: function(item){
    // get the template from the item... or wherever else it comes from
    return new MyViewType({
      template: item.get("the-template")
    });
  }


});

Hope that does what you need

Provoke answered 21/3, 2013 at 13:30 Comment(0)
W
2

First of all i'd like to thanks for everybody who tried to help me. I resolved my own problem. Here is the sollution, if somebody need it:

define(['models/ComponentModel'], function (model) {

    var ItemView = Backbone.Marionette.ItemView.extend({
        model: model,
        render: function() {
            var that = this;
            var data = this.serializeData();

            require(['text!templates/components/editor/' + that.model.get('editor_template') + '.html'], function(Template){
                var html = _.template(Template, data);
                that.$el.html(html);
            });
        }
    });

    return ItemView;
});

edited: (Better sollution)

Suggestions are welcome!

Welby answered 21/3, 2013 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.