$(document).ready() のように実行したいスクリプト (js) があります。スクリプトをテンプレート(例: home_tpl.html)ファイルに書く方法があります。しかし、これは良い方法ではないと確信しています。
バックボーン ビューから html を読み込んでいます。marionate のような他のラッパーは使用していません。ここでは、テンプレートがロードされたとき (DOM がロードされたとき) にスクリプトを実行したいと考えています。スクリプトはどのように記述できますか?
これがビューのレンダリングです
reset: function(key, email){
require(['js/views/reset_password', 'js/models/forgot_password'], function(ResetView, ResetModel){
var resetModel = new ResetModel();
resetModel.set('key', key);
resetModel.set('email', email);
$('body').html(new ResetView({model: resetModel}).render().el);
});
},
ここにビューコードがあります
define(['text!tpl/reset_passwordtpl.html'],function(Template){
return Backbone.View.extend({
template: _.template(Template),
render: function(){
$(this.el).html(this.template());
return this;
},
events: {
"click #btn_reset_password": "reset"
},
reset: function(){
if($('#reset_password').val() != $('#confirm_reset_password').val()){
$('#error_message').text('Passwords mismatched').show();
}
else{
$.ajax({
url: server_url + 'reset',
type:'POST',
dataType:"json",
data: {'id': this.model.get('email'), 'key': this.model.get('key'), 'new_password': $('#reset_password').val()},
success:function (data) {
if(data.error) { // If there is an error, show the error messages
$('.alert-error').text(data.error.text).show();
}
else { // If not, send them back to the home page
$("#content").html("<h6>Your password is reset. Click <a href='#login'>here</a> to login.</h6>");
}
}
});
}
}
});
});
ありがとう