简体   繁体   English

Backbone Marionette在模板示例中渲染模型

[英]Backbone Marionette rendering a model in a template example

I'm converting my backbone example to now extend Marionette. 我正在将我的主干示例转换为现在扩展Marionette。 I'm finding it difficult understand achieving the same thing with templates. 我发现很难理解用模板实现同样的东西。 Let me explain. 让我解释。

This is how I use to render a template 这是我用来渲染模板的方式

In view render function: 在视图渲染功能中:

campaign.fetch({
 var template = _.template(campaignTemplate, {campaign: campaign});
 that.$el.html(template);  

With backbone.marionette I'm not sure how to do the same thing, this is what I have tried without any joy: 使用backbone.marionette我不知道如何做同样的事情, 这是我没有任何喜悦的尝试:

  var campaginView = Backbone.Marionette.ItemView.extend({



        initialize: function (options) {
            // campaign id passed from the URL Route
            this.campaign_id = options.id;
        },

        model: new CampaginModel({
            id: this.campaign_id
        }),

        template: campaignTemplate({
           campaign: this.model.fetch() 
        }),



    }); // end campagin view

* What I'm I doing wrong? * 我做错了什么? underscore is not even there! 下划线甚至没有! * *

Marionette does not require you to pass the model to the template, that is a repetitive task that you need to do in all the Backbone views, and One of the ideas behind Marionette is to reduce boilerplate code. Marionette不要求您将模型传递给模板,这是您需要在所有Backbone视图中执行的重复任务,而Marionette背后的一个想法是减少样板代码。

 var campaginView = Backbone.Marionette.ItemView.extend({

    initialize: function (options) {
        // campaign id passed from the URL Route
        this.campaign_id = options.id;
        this.model = new CampaingModel({id:this.campaign_id});
        this.model.fetch();
    },
    template: campaignTemplate,

});

A great resource of clear and concise examples is the Marionette documentation, please take a look a this link https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.itemview.md Marionette文档中提供了大量清晰简洁的示例资源,请查看此链接https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.itemview.md

also I created a small jsfiddle for this 我也为此创建了一个小jsfiddle

http://jsfiddle.net/rayweb_on/msWvV/ http://jsfiddle.net/rayweb_on/msWvV/

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

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