How do i Cache DOM elements with backbone.js?
In the following code snippet, do i need to cache $("#loading") in another object (lets call it JqueryDOMCache) or Backbone does this for me? I want to make the app performant as I have 300 odd DOM interactions.
var BeforeApploadView = Backbone.View.extend({
el : "#loading",
开发者_开发百科 render : function(){
$(this.el).show();
},
hide : function(){
$(this.el).hide();
}
});
var App = (function($){
var app = {};
app.start = function(s){
var beforeLoadView =new BeforeApploadView();
beforeLoadView.render();
};
return app;
});
No, you do not need to do anything special. There are two reasons for this:
- As long as you still have a reference to your
BeforeApploadView
, then it has a reference toel
so that it stays alive. - The jQuery
hide
function does not remove the elmenet from the DOM. It only appliesstyle="display: none"
to the element. The DOM keeps a reference to the element as well.
You should be all set.
精彩评论