2013-09-30 70 views
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.... 

回答

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(); 

我希望,代碼會幫助你!