【问题标题】:inject a template into another template将一个模板注入另一个模板
【发布时间】:2015-06-24 15:24:30
【问题描述】:

我有一个 Backbone Marionette ItemView,它是父级并且有它自己的模板。 我有一个具有不同模板的视图的子视图。

我想让子模板在某个位置注入父模板,以便父模板“包裹”子模板。

define( ['backbone', 'marionette', 'jquery', 'hbs!templates/partials/parentContents' ],
    function(Backbone, Marionette, $, template) {
    "use strict";

        var ParentView = Backbone.Marionette.ItemView.extend({
            template: template,
            /* // contents of parent template
            <div class="parent">
                <div class="parent-head"></div>
                <div class="parent-body"></div>
            </div>
            */
            events: {
                "click .something": "doSomething"
            },
            initialize:  function(){
                var self = this;
                // some initialization code
            }
        }); 

        // extend events to child class
        ParentView.extend = function(child) {
            var view = Backbone.Marionette.ItemView.extend.apply(this, arguments);
            view.prototype.events = _.extend({}, this.prototype.events, child.events);
            return view;
        };
    }
);

孩子:

define(['jquery', 'hbs!templates/childView', 'backbone', 'views/cards/CardView'],
    function ($, template, Backbone, ParentView) {
        "use strict";

        return ParentView.extend({

            initialize: function(){
                var self = this;
                // do some other stuff
            },

            template: ????,  // I want to get the parent's template and inject this template into it at "parent-body"

            events: {
                'keypress #something': 'doSomethingElse'
            }

        });
    }
);

我想要一个只使用继承而不是渲染方法的解决方案,但两者都可以。

【问题讨论】:

    标签: javascript templates backbone.js handlebars.js


    【解决方案1】:

    我不使用 Marionette,但概念是一样的。

    var ChildView = Backbone.View.extend({
        tagName: "div",
        render: function () {
            var that = this;
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });
    
    
    var MasterView = Backbone.View.extend({
        el: "#master-element",
        render: function () {
            var $el = this.$el.find("#child-holder");
            var view = new ChildView({model: something});
            $el.html(view.render().$el);
        }
    });
    

    以下是我通常使用继承处理视图的方式:

    var RootView = Backbone.View.extend({
        template: function(data){
            return JST[this.templateName](data);
        },
        tagName: "div",
        render: function () {
            var that = this;
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }   
    });
    
    
    var ChildView = RootView.extend({
        templateName: "child-view",
        tagName: "div"
    });
    
    
    var MasterView = RootView.extend({
        el: "#master-element",
        templateName: "master-view",
        render: function () {
            RootView.prototype.render.apply(this, arguments);
            var $el = this.$el.find("#child-holder");
            var view = new ChildView({model: something});
            $el.html(view.render().$el);
        }
    });
    

    【讨论】:

    • 谢谢,我采用了这个概念,并使用了您建议的渲染方法以及把手将标记注入父级,然后正确渲染。不是我想要的方式,但效果很好。 (我一直在寻找一种解决方案,它涉及到继承来获取模板并对 em 做一些事情)
    • 我更新了我的答案,向您展示了一种使用视图继承的方法。在示例中,我使用预编译的车把视图。
    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 2015-11-30
    • 2018-02-02
    • 1970-01-01
    • 2019-09-12
    • 2016-04-22
    • 1970-01-01
    相关资源
    最近更新 更多