简体   繁体   English

骨干主体元素视图未渲染

[英]Backbone Body element View not Rendering

I'm having some issues with Backbone View rendering. 我在使用Backbone View渲染时遇到一些问题。 Could anyone point at my mistake? 谁能指出我的错误?

var BookView = Backbone.View.extend({
    template: _.template('<strong><%= title %></strong> - <%= author %>'),
    tagName: 'div',
    className: 'cover',
    id: '',
    initialize: function() {
        console.log('View initialized');
        this.render();
    },
    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});

var instance = new Book({
    title: 'Thinking, Fast and Slow',
    author: 'Daniel Kahneman',
    issued: '2011'
});
console.log(instance.get('title') + ' by ' + instance.get('author') + ' added to catalogue');

var something = new BookView({model: instance, el: $('body')});
something.render();

The thing is that I'm reading documentation as well as Backbone Fundamentals book and not getting why after el: $('body') View is not appended to body tag. 问题是,我正在阅读文档以及《骨干基础知识》一书,但在el: $('body')后面却找不到原因el: $('body')视图未附加至body标签。

At the same time $('body').append(something.el); 同时$('body').append(something.el); from console works perfectly, but doesn't make me feel that I understand framework concept. 从控制台工作完美,但并没有让我觉得我了解框架概念。

You can't use the el property along with other properties like tagName . 您不能将el属性与其他属性(例如tagName一起使用。

el property is used to to point to an existing element in DOM to which the view is associated. el属性用于指向与视图关联的DOM中的现有元素。

So if you pass el: $('body') in options but then specify tagName: 'div' in view, obviously it doesn't make sense. 因此,如果在选项中传递el: $('body') ,然后在视图中指定tagName: 'div' ,显然这没有任何意义。

So if you want backbone to create a new element for the view, use properties such as tagName , className , id . 因此,如果您希望骨干为视图创建一个新元素,请使用诸如tagNameclassNameid属性。 You should then manually append this view pointing to newly created element to DOM. 然后,您应该手动将指向新创建的元素的视图附加到DOM。

Else use el property to point to an element that already exists in DOM. 其他使用el属性指向DOM中已经存在的元素。

To extend briefly on what @TJ has mentioned, it's worthwhile noting that there is difference between el and $el . 为了简短地介绍@TJ所提到的内容,值得注意的是el$el之间是有区别的。

$el is a cached jQuery (or Zepto) reference to the view's element: (Since v0.9.0 ) $el是对视图元素的缓存jQuery(或Zepto)引用:(从v0.9.0开始

A cached jQuery object for the view's element. 视图元素的缓存jQuery对象。 A handy reference instead of re-wrapping the DOM element all the time. 一个方便的引用,而不是始终重新包装DOM元素。

this.el can be resolved from a DOM selector string or an Element: 可以从DOM选择器字符串或Element解析this.el

this.el can be resolved from a DOM selector string or an Element; 可以从DOM选择器字符串或Element解析this.el otherwise it will be created from the view's tagName , className , id and attributes properties. 否则,它将根据视图的tagNameclassNameidattributes属性创建。 If none are set, this.el is an empty div 如果未设置,则this.el为空div


To get you current code working , all you need to do is remove the tagName , id and className properties: 为了使当前代码正常工作 ,您需要做的就是删除tagNameidclassName属性:

var BookView = Backbone.View.extend({
    template: _.template('<strong><%= title %></strong> - <%= author %>'),
    initialize: function() {
        this.render();
    },
    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});

Since you are calling render() from initialize , you don't need to call it again after it has been initialized: 由于您是从initialize调用render() ,因此您无需在初始化后再次调用它:

var something = new BookView({model: instance, el: $('body')});


Here is a Fiddle with your working code 这是您的工作代码的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM