2017-01-04 8 views
-1

_bindall()を実行した後でも、this.modelがフォームのsubmitイベント関数で未定義を返すようです。レンダリング機能は正常です。フォーム送信イベントのバックボーンモデル

initialize: function() { 
    _.bindAll(
    this, 
    "render", 
    "processFormEditJobSubmit" 
); 
}, 

フォームがイベントを提出する:これを解決する方法

processFormEditJobSubmit: function(e) { 
    e.preventDefault(); 
    e.stopImmediatePropagation(); 
    console.log(this.model); //undefined 
} 

?あるいは私は拘束力に間違いを犯しましたか?私はprollyは、sessionStorageでモデルをシリアライズし、それをsubmit関数でデシリアライズします。私は自分のコードでハッキングを避けることを好む。何か指摘していただきありがとうございます。

答えて

0

ありますが示されたコードに誤りがありませんが、例としては不完全である、ここで働いてコードスニペットです:私が見逃している何かがある場合たとえば、

var MyView = Backbone.View.extend({ 
 

 
    el: '#view', 
 

 
    events: { 
 
    'click button.a': 'processFormEditJobSubmit' 
 
    }, 
 

 
    initialize: function() { 
 
    _.bindAll(
 
     this, 
 
     "render", 
 
     "processFormEditJobSubmit" 
 
    ); 
 
    }, 
 
    
 
    render: function() { 
 
    this.$('button.b').click(this.processFormEditJobSubmit); 
 
    }, 
 

 
    processFormEditJobSubmit: function(e) { 
 
    e.preventDefault(); 
 
    e.stopImmediatePropagation(); 
 
    console.log(this.model.get('prop')); //not undefined 
 
    } 
 

 
}); 
 

 
new MyView({ 
 
    model: new Backbone.Model({prop: 'here'}) 
 
}).render();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script> 
 

 
<div id="view"> 
 
    <button class="a"> 
 
    event hash 
 
    </button> 
 
    <button class="b"> 
 
    jquery handler 
 
    </button> 
 
</div>

だから私はスニペットを更新します。

関連する問題