1

私はこのようなビューを持っています:

var InvoiceForm = Backbone.View.extend({
    template: _.template(addform),
    initialize: function () {
        this.$("#buyer").val("test");
    }
});

購入者 ID 要素を含むテンプレート addform があります。これは値を設定していません。私はここで愚かなことをしていることを知っています:)

4

2 に答える 2

2

ビューのバックボーンテンプレートは自動的にレンダリングされません。実際、バックボーンのドキュメントには、それを行う方法の例がいくつかあります。

ビューでテンプレートを使用する場合は、初期化時にレンダリングする必要があり、次のようにコードを記述できます。

var InvoiceForm = Backbone.View.extend({
    template: _.template(addform),
    render: function() {
        this.$el.html(this.template());
        return this;
    },
    initialize: function () {
        this.render(); // Rendering your template in your this.el element
        this.$("#buyer").val("test"); // Setting the value of #buyer in your template
    }
});

後でこのビューを次のように使用できます。

var form = new InvoiceForm({ el: $("#wrapper_for_invoiceform") });

このフィドルのデモを確認してください:http://jsfiddle.net/hsn7J/

于 2013-02-09T08:54:40.920 に答える
1

クリックイベントでこれを実行しようとしている場合-'test'値に変更するには、次の方法で実行できます。

initialize: function(){
            this.model.on('change', this.render, this);         
        },

    render: function() {
            this.$el.html( this.template(this.model.toJSON()) );
            this.input = this.$('#buyer');
            return this;
        },

    events: {
     'click #buyer' : 'editValue',
    },

    editValue: function(){
        this.input.val("test");
    }
于 2013-02-09T09:01:54.103 に答える