简体   繁体   English

是否可以避免使用Django和Backbone.js双重模板?

[英]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. 我有一个复杂的不是SPA(不是单页应用程序)的经典多页站点,我想在其中使用Backbone.js。

The server side of the app is Django powered. 该应用程序的服务器端由Django驱动。

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 . 我的问题是:出于SEO的原因, 我必须使用django模板加载服务器端的每个html内容 ,但是要获得Backbone的全部功能, 我需要使用下划线的模板(或车把)来刷新主干网的视图

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. 实际上,您可以编写单页应用程序,并且仍然支持SEO。 Backbone's router accomplishes this by creating a separate URL for each state of the application. 骨干路由器通过为应用程序的每种状态创建一个单独的URL来实现此目的。 Your links throughout the application will be crawled. 您在整个应用程序中的链接将被爬网。 Google does a good job of crawling SPA's these days. 如今,Google在抓取SPA方面做得很好。 I believe your decision not to create your site as an SPA was influenced by stale opinion. 我认为您决定不将自己的网站创建为SPA的决定受到了过时意见的影响。

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 说我的页面有这个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 实例化使用,但不调用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. 每当您要更新视图时,请调用myView.render。 By this you get the benefit of SEO and backbone both. 通过这种方式,您可以获得SEO和骨干网的好处。 I guess popular web applications like You Tube does follow similar approach (may not be using Backbone). 我猜像You Tube这样的流行Web应用程序确实遵循类似的方法(可能未使用Backbone)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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