簡體   English   中英

重定向或呈現事件的其他視圖

[英]Redirect or render another view on event

我正在Backbone中構建一個小的CRUD應用程序,並且我有點需要從一個視圖重定向到另一個視圖。 我的應用程序包含一個layout視圖(其中包含其他視圖)和一個路由器。 這里是:

var router = Backbone.Router.extend({
     routes: {
         '': 'home',
         'resumes/:id': 'showResume'
     },

     home: function () {
         // renders a index view with my collection
         this.layout.render(new ResumeList({collection: resumes});
     },
     showResume: function () {
         if (!this.fullResume) {
             this.fullResume = new FullResume({model: new Resume()});
         }
         // allowing to navigate via url with model id
         this.fullResume.model.set('id', id).fetch({
             context: this,
             success: function () {
                 this.layout.render(this.fullResume);
             }
         });
     }
});

然后,在我的FullResume視圖中,我有一個delete事件,該事件破壞了模型。 它去了:

var FullResume = Backbone.View.extend({
    // tagName and other stuff
    events: {
        // other events
        'click #delete': 'deleteResume'
    },
    // initialize, render and other functions
    deleteResume: function () {
        this.model.destroy({
            success: function (res) {
                console.log('DELETE model' + res.toJSON().id);
            },
            error: function () {
                console.log('Failed to DELETE');
            }
        });
    }
});

上面的功能可以完美工作並刪除模型,但是刪除模型后,它仍然保留在視圖中,直到我手動導航到某個地方。 我閱讀了一下,嘗試管理如何在此事件發生后或重定向到主視圖后呈現主視圖,但並沒有成功。

您正在尋找將trigger選項設置為true的http://backbonejs.org/#Router-navigate函數。

這是一個示例: http : //jsfiddle.net/x3t7u5p0/

單擊“主頁”或“關於”鏈接將更改視圖,但是我添加了一個延遲的程序化視圖更改,當“關於”視圖呈現時,在延遲后它將切換回主頁

  render: function () {
      this.$el.html(this.template);
      _.delay(function() {
          appRouter.navigate('home', {trigger: true});
      }, 500);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM