简体   繁体   中英

Is having Django and Backbone.js double templates avoidable?

I have a complex not SPA (not single page application), a classic multi-page site, in which i'd like to use Backbone.js.

The server side of the app is Django powered.

My problem is: for SEO reasons I have to load every html content server-side, using django templates , but then to get Backbone's full power I need underscore's template (or handlebars) to refresh my backbone's views .

So, i have to write the same templates twice, with different tecnologies and hooks.

How can I avoid this?

Actually, you can write a single-page application and still support SEO. Backbone's router accomplishes this by creating a separate URL for each state of the application. Your links throughout the application will be crawled. Google does a good job of crawling SPA's these days. I believe your decision not to create your site as an SPA was influenced by stale opinion.

Basically you can do that, All you need to make sure is you don't call render on views first time.

say my page has this html

<ul class="my-list">
 <li><a href="#">do something</a></li>
 <li><a href="#">do something</a></li>
 <li><a href="#">do something</a></li>
</ul>

initially you define a view

var MyView =  Backbone.View.extend({
  el:'.my-list',
  render:function(){
    this.collection.each(this.addItem, this);
  },
  addItem:function(){
    //do adding logic here
  }
})

instantiate using, but don't call render

 var myView = new MyView({
    collection: myCollection //any collection or model you like here
 })

when ever you want to update the view, call myView.render then. By this you get the benefit of SEO and backbone both. I guess popular web applications like You Tube does follow similar approach (may not be using Backbone).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM