【问题标题】:Backbone - Issue when managing page transitionsBackbone - 管理页面转换时的问题
【发布时间】:2013-07-01 12:02:25
【问题描述】:

我正在尝试使用 Derick Bailey 在此 article 中的建议来管理主干中的页面转换,但 about 路由未呈现。它只用 app.BookListView 渲染 home 路由,当我点击访问 about 路由时,它仍然是 app.BookListView。

这是我的代码:

HTML

<section class="feed">
</section> 
<script id="bookTemplate" type="text/template">

        <div class="book">
        </div>

</script>

 <script id="aboutTemplate" type="text/template">

         <div class="about">
         Bla bla bla bla
         </div>

  </script>

观点

Backbone.View.prototype.close = function(){
   this.remove();
   this.unbind();
};

function AppView(){
    this.showView(view) {

        if (this.currentView){
        this.currentView.close();
        }

        this.currentView = view;
        this.currentView.render();
        $('.feed').html(this.currentView.el);
     }
 };

app.BookView = Backbone.View.extend ({
    tagName: 'div',
    className: 'book', 

template: _.template( $( '#bookTemplate' ).html()),

render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    return this;
}
)};

app.BookListView = Backbone.View.extend({
    el: '.feed',
    initialize: function ( initialBooks ) {
        this.collection = new app.BookList (initialBooks);
        this.render();
     },

    render: function() {
         this.collection.each(function( item ){
                this.renderBook( item );
         }, this);
     },

    renderBook: function ( item ) {
         var bookview = new app.BookView ({
              model: item
         })
         this.$el.append( bookview.render().el );
         } 
    });

 app.AboutView = Backbone.View.extend({
     tagName: 'div',
     className: 'about',

     template: _.template( $( '#aboutTemplate' ).html()),

     render: function () {
         this.$el.html(this.template());
         return this;
     }
 });

路由器

var AppRouter = Backbone.Router.extend({
    routes: {
        '' : 'home',
        'about' : 'about'
    },

    initialize: function(options){
        this.AppView = options.AppView;
    },

    home: function () {
         var homeView = new app.BookListView();
    this.AppView.showView(homeView);
    },

    about: function () {
          var aboutView = new app.AboutView();
          this.AppView.showView(aboutView);
    }
});

app.Router = new AppRouter();
Backbone.history.start();

还有一些测试 BookView 的数据:

$(function(){
    var books = [
        {title:'Imperial Bedrooms'},
        {title:'Less than Zero'},
     ];
new app.BookListView (books);

JSFiddle:http://jsfiddle.net/swayziak/xdRRE/

控制台抛出此错误:

  • Uncaught SyntaxError: Unexpected token { , in "this.showView(view) {"

  • Uncaught TypeError: Cannot read property 'AppView' of undefined , in "this.AppView = options.AppView"

谢谢。

【问题讨论】:

  • 你试过 jslint 了吗?
  • 是的,但它没有显示任何可能与此问题相关的错误

标签: javascript backbone.js marionette backbone-views backbone-routing


【解决方案1】:

您有语法错误、....错位或缺少字符、未定义的对象。

new app.BookListView (books);

var bookListView  =  new app.BookListView (books);

以下将不起作用:

  this.showView(view) {

    if (this.currentView){…

我猜你想要这个?

this.showView = function(view){…}

以下行也抛出错误:

app.BookView = Backbone.View.extend ({…)};

应该是:

app.BookView = Backbone.View.extend ({…});

下一行工作:

this.AppView.showView(homeView);

你必须在初始化时在这里传递appview

app.Router = new AppRouter(appView);

我会尝试清理您的小提琴,您使用的是哪种代码编辑器?因为所有错误都与语法有关。

更新

这是fiddle

你家路线的问题出在这一行:

home: function () {
    if(!this.bookListView){
        this.bookListView = new app.BookListView();
    }
},

应该是:

home: function () {
    if(!this.bookListView){
        this.bookListView = new app.BookListView(books);
    }else{
        this.bookListView.render();
    }
},

【讨论】:

  • 我正在使用 Sublime Text 2。感谢您的回答。我已经进行了所有更改,但仍然无法正常工作。
  • 你的jsFiddle里的代码,就是你所有的代码吗?我们能看到所有的代码,然后我可以离线查看,jsFiddle不是适合这项工作的工具。
  • 我有更多的 html 侧边栏,其中包含关于标记、模型、集合和 Javascript 中的条款和视图(其代码等于关于视图)。我认为问题与 app.BookListView 有关,因为我已经尝试在路由器中使用另一个代码并且 About 和 Privacy 路由正常工作,我的主路由有问题,这是另一个路由器的 JSFiddle:@987654322 @。也许我可以通过电子邮件或其他方式将代码发送给您。谢谢。
  • 当然,电子邮件在我的用户页面上。
  • 谢谢!但是在您的用户页面中,我只看到您的年龄和您的位置,但我没有看到您的电子邮件
猜你喜欢
  • 2012-07-05
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
相关资源
最近更新 更多