EL 是强制性的还是我们可以使用另一个 属性 名称?
EL mandatory or can we use another property name?
尽我所能学习BackboneJS。有人告诉我 "el" 很特别。然而,看看下面的代码,看起来我可以使用任何我喜欢的 属性 名称。例如:
MyEL : $('body')
那我就这样用
$(this.MyEL).append("<ul></ul>");
不使用el算违规吗?我是不是弄坏了什么?
(function($){
var ListView = Backbone.View.extend({
el: $('body'), // el attaches to existing element
myEL: $('body'), // myEL attaches to existing element
events: {
'click button#add': 'addItem'
},
initialize: function(){
_.bindAll(this, 'render', 'addItem'); // every function that uses 'this' as the current object should be in here
this.counter = 0; // total number of items added thus far
this.render();
},
// `render()` now introduces a button to add a new list item.
render: function(){
// $(this.el).append("<button id='add'>Add list item</button>");
// $(this.el).append("<ul></ul>");
$(this.myEL).append("<button id='add'>Add list item</button>");
$(this.myEL).append("<ul></ul>");
},
// `addItem()`: Custom function called via `click` event above.
addItem: function(){
this.counter++;
$('ul', this.el).append("<li>hello world"+this.counter+"</li>");
}
});
var listView = new ListView();
})(jQuery);
如果您查看代码,您正在做的是 扩展 (Backbone.View.extend({//options})
) 默认的 backbone 视图对象,具有您想要的任何属性加上。如果您传递 backbone 视图中默认存在的 属性,它将被您传递的值覆盖。
默认情况下,所有 backbone 视图都有一个 el
属性。如果您没有在选项中明确传递它的值,backbone 将创建一个 <div>
并将其分配给 el
属性.
当您在选项中传递 el: $('body')
时,backbone 会将默认的 el
属性 指向 <body>
并且不会创建 <div>
.
通过在选项中传递 myEL: $('body')
,您在名为 myEL
的视图对象中创建了一个 属性,它指向 <body>
元素,在本例中好像没必要。
尽我所能学习BackboneJS。有人告诉我 "el" 很特别。然而,看看下面的代码,看起来我可以使用任何我喜欢的 属性 名称。例如:
MyEL : $('body')
那我就这样用
$(this.MyEL).append("<ul></ul>");
不使用el算违规吗?我是不是弄坏了什么?
(function($){
var ListView = Backbone.View.extend({
el: $('body'), // el attaches to existing element
myEL: $('body'), // myEL attaches to existing element
events: {
'click button#add': 'addItem'
},
initialize: function(){
_.bindAll(this, 'render', 'addItem'); // every function that uses 'this' as the current object should be in here
this.counter = 0; // total number of items added thus far
this.render();
},
// `render()` now introduces a button to add a new list item.
render: function(){
// $(this.el).append("<button id='add'>Add list item</button>");
// $(this.el).append("<ul></ul>");
$(this.myEL).append("<button id='add'>Add list item</button>");
$(this.myEL).append("<ul></ul>");
},
// `addItem()`: Custom function called via `click` event above.
addItem: function(){
this.counter++;
$('ul', this.el).append("<li>hello world"+this.counter+"</li>");
}
});
var listView = new ListView();
})(jQuery);
如果您查看代码,您正在做的是 扩展 (Backbone.View.extend({//options})
) 默认的 backbone 视图对象,具有您想要的任何属性加上。如果您传递 backbone 视图中默认存在的 属性,它将被您传递的值覆盖。
默认情况下,所有 backbone 视图都有一个 el
属性。如果您没有在选项中明确传递它的值,backbone 将创建一个 <div>
并将其分配给 el
属性.
当您在选项中传递 el: $('body')
时,backbone 会将默认的 el
属性 指向 <body>
并且不会创建 <div>
.
通过在选项中传递 myEL: $('body')
,您在名为 myEL
的视图对象中创建了一个 属性,它指向 <body>
元素,在本例中好像没必要。