Backbone.js 是否基于数据中的变量指定视图样板?
我使用的是主干网和木偶网,我想根据数据中的变量渲染视图。我想这个.model.template可以从我的数据中提取(返回myTemplate和MyTherTemplate),然后我可以在渲染函数中进行一些操作,但它不起作用。有什么建议吗?。可以让视图知道模型吗Backbone.js 是否基于数据中的变量指定视图样板?,backbone.js,marionette,Backbone.js,Marionette,我使用的是主干网和木偶网,我想根据数据中的变量渲染视图。我想这个.model.template可以从我的数据中提取(返回myTemplate和MyTherTemplate),然后我可以在渲染函数中进行一些操作,但它不起作用。有什么建议吗?。可以让视图知道模型吗 var graph = [{ nodeName: "1st level item", template: "myTemplate", nodes: [{ nodeName: "2nd level i
var graph = [{
nodeName: "1st level item",
template: "myTemplate",
nodes: [{
nodeName: "2nd level item",
template: "myOtherTemplate"
}]
}];
TreeView = Backbone.Marionette.CompositeView.extend({
tagName: "ul",
initialize: function(){
this.collection = this.model.nodes;
},
appendHtml: function(collectionView, itemView){
collectionView.$("li:first").append(itemView.el);
},
render: function(){
var that = this;
console.log('Loading template name: ' + name + ' template: ' + this.template + ' data: ' + this.model.template);
TemplateManager.get(this.template, function(template){
var html = $(template).tmpl();
that.$el.html(html);
});
return this;
}
});
如何初始化视图 视图通常期望模型是主干模型。访问模型属性时,应使用
mymodel.get('attributeName')
。单个属性不能直接在模型上使用。它们可以在mymodel.attributes
(例如,mymodel.attributes.template
)中找到,但是除了调试之外,不应该直接使用属性
,因为属性的存储和访问方式将来可能会发生变化,或者如果您使用任何插件,它们可能会被各种插件改变
还请注意,在大多数情况下,不需要重写
render
方法。相反,请在渲染前查看和onRender
我很难理解您的问题是什么。可以让视图知道模型吗?当然:-)只需执行:this.model
即可从其中引用视图的模型。除此之外,尽管我不明白您的要求。this.model.template为我返回undefined:(this.model将是您传递到视图中的任何模型。因此,在您的代码中必须有新的TreeView({…
;在省略号中,您可以定义一个模型,如:新的TreeView{model:new Backbone.model(图形)
综上所述,模型通常应该表示数据;模板实际上属于视图的属性,而不是模型的属性。