2015-10-07 175 views
2

可否請你告訴我如何從一個頁面導航到另一個主頁。 我想顯示按鈕第二HTML點擊它是如何可能如何將一個視圖移動到另一個視圖?

我那麼喜歡這樣的

events: { 
     'click #click':'moveTonext' 
     }, 

     moveTonext: function(){ 
      alert('---') 
     }, 

說。我電阻事件我做第二頁這樣的

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'text!templates/second.html' 
], function ($, _, Backbone, statsTemplate) { 
    'use strict'; 

    var secondView = Backbone.View.extend({ 

     // Instead of generating a new element, bind to the existing skeleton of 
     // the App already present in the HTML. 
     el: '#todoapp', 

     // Compile our stats template 
     template: _.template(statsTemplate), 

     // Delegated events for creating new items, and clearing completed ones. 
     events: { 

     }, 



     // At initialization we bind to the relevant events on the `Todos` 
     // collection, when items are added or changed. Kick things off by 
     // loading any preexisting todos that might be saved in *localStorage*. 
     initialize: function() { 
      this.render(); 
     }, 

     serialize: function() { 
      return { 
      message: 'world' 
      }; 
     }, 

     // Re-rendering the App just means refreshing the statistics -- the rest 
     // of the app doesn't change. 
     render: function() { 
      this.$el.html(this.template()); 
     } 
     // Add a single todo item to the list by creating a view for it, and 
     // appending its element to the `<ul>`. 



    }); 

    return secondView; 

}) 

另一個HTML

<h1>second</h1> 

這裏是我的獵人 http://plnkr.co/edit/fCXwSrroJP1l6BppjpmD?p=preview

回答

0

基本上你的按鈕會觸發導航,所以click處理程序應該是這樣的:

moveToNext: function() { 
    router.navigate("other/path", { trigger: true }); 
} 

然後,在你router代碼,你需要添加一個路由處理程序對上述路徑:

routes: { 
    "other/path": "handleOtherPath" 
}, 

handleOtherPath: function() { 
    new SecondView(); 
} 

這是針對SecondView應取代FirstView的情況。如果應該添加以下機制,則可以使用以下機制:

moveToNext: function() { 
    new SecondView({ el: this.$(secondViewContainerSelector) }); 
} 

這是Plunker sample的工作。

+0

請問您可以給予活板.. – user944513

+0

請提供活塞或編輯活塞 – user944513

+0

@ user944513我更新了答案以包含鏈接到活塞。 – Dethariel

相關問題