jashkenas / backbone

Give your JS App some Backbone with Models, Views, Collections, and Events
http://backbonejs.org
MIT License
28.1k stars 5.39k forks source link

Passing non-existant selector to Backbone.View #4161

Closed cloakedninjas closed 6 years ago

cloakedninjas commented 6 years ago

This is more of a question really...

Given this fiddle: https://jsfiddle.net/oowrs2gh/ (copied below also)

var MyView = Backbone.View.extend({
});

var view = new MyView({
    el: '#foo-bar-bat'
});

console.log(view.el);

The el is undefined. And I wonder if perhaps _ensureElement should fallback to creating an element if the selector fails?

paulfalgout commented 6 years ago

This issue was recent raised with Marionette as well as currently what happens is nothing. The view appears to work and if you adhere to jQuery for rendering and appending, there may not be any error given, you'll just end up with a blank view where you were expecting it to be prefilled with DOM. I don't really think that creating an element is the ideal solution as it doesn't really follow what I imagine the intention is of setting the el as a selector. Honestly I'd prefer that el didn't take a selector at all.. it's easy enough to give the view an el... but since it does take a selector, I think it'd be better to error if it doesn't find it.

jashkenas commented 6 years ago

That's working as intended. If you pass a selector to view.el, it needs to exist. Otherwise, why pass it at all?