{"id":5606,"date":"2014-04-03T21:36:57","date_gmt":"2014-04-03T21:36:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/03\/ember-js-prevent-re-render-when-switching-route-collection-of-common-programming-errors\/"},"modified":"2014-04-03T21:36:57","modified_gmt":"2014-04-03T21:36:57","slug":"ember-js-prevent-re-render-when-switching-route-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/03\/ember-js-prevent-re-render-when-switching-route-collection-of-common-programming-errors\/","title":{"rendered":"Ember.js &#8211; Prevent re-render when switching route-Collection of common programming errors"},"content":{"rendered":"<p>Simplified demo: http:\/\/jsfiddle.net\/indream\/KskXx\/<br \/>\n(this demo cannot simulate actual environment with my problem)<\/p>\n<p>For the demo: Hover on photos will show you the caption.<br \/>\nWhen you clicked a photo, route changed to &#8216;media&#8217; with <code>{{linkTo}}<\/code> helper &amp; lightbox opened.<br \/>\nWhen clicked place outside lightbox, route changed back to &#8216;feed&#8217; by <code>history API<\/code> &amp; lightbox closed.<\/p>\n<p><strong>My question<\/strong>: Template <strong>re-rendered<\/strong> when switch back to &#8216;feed&#8217;.<br \/>\n<strike>(You can check it by hover on photos as caption lost after that.)<\/strike><br \/>\nI want to stop this as the app lagged during re-render if there&#8217;re lots of photos.<\/p>\n<p><em><strong>Using <code>{{linkTo}}<\/code> is the reason of problem, please refer to my answer<\/strong><\/em><\/p>\n<p>I&#8217;ve read servel releated question like Ember.js &#8211; currentViewBinding and stop re-rendering on every view transition and Animating views before destruction in Emberjs.<br \/>\nBut the methods provided seems not work for RC2, I&#8217;ve tried to modify <code>willDestroy<\/code> event, it works for not re-render but it throwed:<br \/>\n<code>Uncaught Error: Cannot perform operations on a Metamorph that is not in the DOM.<\/code><br \/>\n<code>Uncaught Error: NotFoundError: DOM Exception 8<\/code><br \/>\nwhen I switched to another route (i.e. empty <code>nowContent<\/code> for loading other content). And modify <code>destroyElement<\/code> isn&#8217;t work at all.<\/p>\n<p>Here&#8217;s my code, any ideas to solve my problem?<\/p>\n<pre><code>App.MainView = Ember.View.extend({\n  templateName:'main',\n  willDestroy: function() {\n    if (App.get('destroyCurrentView')){\n      this._super();\n    }\n  }\n})\nApp.PageController = Ember.Controller.extend({\n  lightboxClose:function(e){\n    if(!e||e.target==e.currentTarget){\n      $('#lightbox').hide();\n      $('body').removeClass('noscroll');\n      history.back();\n\n      App.set('destroyCurrentView',false);\n      setTimeout(function(){\n        App.set('destroyCurrentView',true);\n      }, 500);\n    }\n});\nApp.MediaRoute = App.mainRoute.extend({\n  enter:function(){\n    App.set('destroyCurrentView',false);\n    this._super();\n  }\n});\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simplified demo: http:\/\/jsfiddle.net\/indream\/KskXx\/ (this demo cannot simulate actual environment with my problem) For the demo: Hover on photos will show you the caption. When you clicked a photo, route changed to &#8216;media&#8217; with {{linkTo}} helper &amp; lightbox opened. When clicked place outside lightbox, route changed back to &#8216;feed&#8217; by history API &amp; lightbox closed. My [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5606","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5606"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5606\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}