开发者

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 to el so that it stays alive.
  • The jQuery hide function does not remove the elmenet from the DOM. It only applies style="display: none" to the element. The DOM keeps a reference to the element as well.

You should be all set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜