2

$(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>");
                    }
                }
            });
        }
    }
});
});

ありがとう

4

1 に答える 1

-1

どこかにメイン ビューが必要です。スタイルシートなども含める場所。ページの下部、body 終了タグの直前。一部のスクリプト タグ内では、document.ready を実行するだけです。それがテンプレートにも含まれていない限り、その場合、ビューに関しては別の構造を使用することを検討します。

1 つのテンプレートの読み込み準備ができたときにこれを実行したい場合は、無名関数で成功を使用します。

于 2013-04-04T12:54:54.977 に答える