2014-12-24 59 views
0

我有一個視圖與Backbone.js創建並插入到div元素 - 它顯示大約一秒,然後消失。骨幹查看顯示然後消失

這裏是我的視圖代碼:

var addPlayerView = Backbone.View.extend({ 
    tagName: "div", 
    model: Player, 
    id: 'addPlayerDiv', 

    initialize: function() { 
     console.log('addPlayerView has been created'); 
    }, 

    render: function(){ 
     this.$el.html('<p>show this puppy</p>'); 
     return this; 
    } 
}); 

這裏是模型:

var Player = Backbone.Model.extend({ 
    defaults: { 
     ID: "", 
     firstName: '', 
     lastName: '' 
    }, 

    idAttribute: "ID" 
}); 

這裏是HTML:

<form onsubmit="addNewPlayer();"> 
     <input type="submit" value="Add New Player New"/> 
</form> 

<p> 
<div id="addPlayerDiv"></div> 
</p> 

<script> 

     function addNewPlayer() { 
      var player = new Player({}); 
      var newPlayerView = new addPlayerView({el: $("#addPlayerDiv"), model: player}); 
      newPlayerView.render(); 
     }; 

</script> 

的addNewPlayer()函數被正確調用,並且newPlayerView在頁面上呈現,但只有一秒鐘,然後它在頁面上消失。

不知道該怎麼做。任何人有過這個問題嗎?

+0

是否頁面重載? –

+0

不,沒有呼叫重新加載頁面,並從我可以告訴它不重新加載 –

回答

3

您需要取消默認的動作(在我們的例子onsubmit試圖將數據發送到服務器)

<form onsubmit="addNewPlayer(); return false;"> 

<form onsubmit="addNewPlayer(event);"> 

function addNewPlayer(e) { 
    e.preventDefault(); 
    ..... 
} 

Example