最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does the 'el' do in view.render().el? - Stack Overflow

programmeradmin5浏览0评论

I am trying to learn backbone. I understand that el is the element that is being acted on. If it isnt specified it is an empty div. I am creating a template inside my view and returning the view object. I am then rendering the view, but I dont understand why I chain el after the render function. Could someone please explain this line of code to me:

var view = new PersonView();
this.$('#family_list').children().append(view.render().el);

What is the el for? Thanks.

I am trying to learn backbone. I understand that el is the element that is being acted on. If it isnt specified it is an empty div. I am creating a template inside my view and returning the view object. I am then rendering the view, but I dont understand why I chain el after the render function. Could someone please explain this line of code to me:

var view = new PersonView();
this.$('#family_list').children().append(view.render().el);

What is the el for? Thanks.

Share Improve this question asked Dec 1, 2012 at 13:02 jhammjhamm 25.1k42 gold badges110 silver badges188 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 21

jQuery's .append() method expects a string of HTML or a DOM element to be appended to its calling node.

The .el property of the view is its bound DOM element. After calling view.render(), its .el DOM element property is passed into the jQuery .append() method, so jQuery .append() gets the updated (newly rendered) DOM node.

This is made possible because the .render() call must be returning this as suggested in the documentation. Its return value is therefore the view object itself1, and from that the .el can be referenced immediately.


1 Wikipedia: Fluent interfaces

发布评论

评论列表(0)

  1. 暂无评论