バックボーン js のイベントに関数をアタッチするときに、 this または $(this) セレクターが機能しないのはなぜでしょうか。このコード例を見てください:
var testView = Backbone.View.extend({
el: $('#test'),
events: {
'keyup #signup-fullname': 'validateFullname'
},
validateFullName: function(e){
if($(this).val() == "mike"){
alert('You are just amazing!');
} else if($(this).val() == "tom"){
alert("mmm.. you fail...")
}
}
});
それは機能しておらず、私がこれを行う場合にのみ機能します:
var testView = Backbone.View.extend({
el: $('#test'),
events: {
'keyup #signup-fullname': 'validateFullname'
},
validateFullName: function(e){
if($('#signup-fullname').val() == "mike"){
alert('You are just amazing!');
} else if($('#signup-fullname').val() == "tom"){
alert("mmm.. you fail...")
}
}
});
this や $(this) でできるのはちょっとやり過ぎじゃないですか?
ありがとう