0

View によってそれぞれレンダリングされた多数の写真を表示するページがありますPhotoListItemViewModalAddToSetView写真をクリックすると、セットのリストを含むモーダル ビューが表示されますSetListView。Set の 1 つSetViewがクリックされたら、photo_idandset_idをバックエンドに送信する必要があります。

問題:set_id Set のクリック ハンドラーで、を使用してクリックされたセットの を簡単に取得できますthis.model.get('id')。を取得する従来の方法は何photo_idですか?

写真の見方

photo_idこのビューに渡されたモデルにある

PhotoListItemView = Backbone.View.extend({

    events: {
        'click #add.photo_btn' : 'add'
    },

    add: function(event) {
        event.stopImmediatePropagation();

        // Show modal
        $('#modal_addit').modal();
        modalAddToSetView = new ModalAddToSetView({ model: this.model });
    }

});

モーダルの見方

ModalAddToSetView = Backbone.View.extend({

    initialize: function() {
        this.render();
        this.renderSets();
    },

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
        return this;
    },

    renderSets: function() {
        this.setList = new SetCollection();
        this.setListView = new SetListView({ collection: this.setList });
        this.setList.fetch({
            data: {user_id: $('#user_id').val()},
            processData: true
        });
    }
});

セットの見方

SetListView = Backbone.View.extend({

    initialize: function() {
        this.collection.on('reset', this.render, this);
    },

    render: function() {
        this.collection.each(function(set, index) {
            $(this.el).append( new SetView({ model: set }).render().el );
        }, this);
    }
});

SetView = Backbone.View.extend({

    template: _.template( $('#tpl_modal_addit_set').html() ),

    events: {
        'click': 'addToSet'
    }

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
        return this;
    },

    addToSet: function() {
        $.post('api/add_to_set', {
            photo_id: ,         // HOW DO I PASS THE PHOTO_ID?
            set_id: this.model.get('id')
        })
    }
});
4

1 に答える 1

0

photo_idparam をSetListViewコンストラクターに渡し、コンストラクターに再度渡すことに問題はありませんSetView

// code simplified and no tested
SetListView = Backbone.View.extend({
  initialize: function( opts ) {
    this.photo_id = opts.photo_id;
    this.collection.on('reset', this.render, this);
  },

  render: function() {
    this.collection.each(function(set, index) {
      $(this.el).append( new SetView({ model: set, photo_id: this.photo_id }).render().el );
    }, this);
  }
});

SetView = Backbone.View.extend({
  initialize: function( opts ) {
    this.photo_id = opts.photo_id;
  },

  addToSet: function() {
    $.post('api/add_to_set', {
      photo_id: this.photo_id,
      set_id: this.model.get('id')
    })
  }
});
于 2012-07-23T14:52:32.357 に答える