骨干与本地化模板

时间:2013-09-30 09:37:52

标签: javascript backbone.js

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

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....

1 个答案:

答案 0 :(得分: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();

我希望,代码可以帮到你!