0

我创建了一个带有本地化模板的主干示例代码。但我无法调试它为什么不起作用。请帮我。代码在以下路径中可用。

http://jsfiddle.net/nagesforyou/kdDMK/

var resource = {localizedHeader : "localized header in english"};

var BaseView = Backbone.View.extend({
    localizedTemplate : function(element) {
        self : this;
        this.template = _.template(element.html());

        return function(data) {
            return self.template($.extend({},data,resource));
        };
    }
});

var DerivedView = BaseView.extend({
    initialize: function(options) {
        this.model = options.model;

        this.template = this.localizedTemplate($("#someTemplate"));
    },

    render : function() {
        var output = this.template(this.model.toJSON());
        this.$el.append(output);
        return this;
    }
});

var someModel = Backbone.Model.extend({

});

var mdl = new someModle({someNmae : "surendhar"});
var derivedView = new DerivedView({model: mdl, el:$('body')});
derivedView.render();


<script type="text/template" id="someTemplate">
    <div>
        Sometext
        <div> <%= localizedHeader%> </div>
        <div> <%= someName %> </div>
    </div>
</script>
Running....
4

1 回答 1

0

使用以下代码:

var DerivedView = BaseView.extend({
    render : function() {
        rendered = $.tmpl($(this.template || this.options.template).template(), this.model.toJSON() );
        this.$el.html(rendered);
    }
});

并这样称呼它:

var derivedView = new DerivedView({model: mdl, el:$('body'), template: "#someTemplate"});
derivedView.render();

我希望,代码对你有帮助!

于 2013-09-30T10:43:20.607 回答