Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 是否基于数据中的变量指定视图样板?_Backbone.js_Marionette - Fatal编程技术网

Backbone.js 是否基于数据中的变量指定视图样板?

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

我使用的是主干网和木偶网,我想根据数据中的变量渲染视图。我想这个.model.template可以从我的数据中提取(返回myTemplate和MyTherTemplate),然后我可以在渲染函数中进行一些操作,但它不起作用。有什么建议吗?。可以让视图知道模型吗

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(图形)
综上所述,模型通常应该表示数据;模板实际上属于视图的属性,而不是模型的属性。