0

我有一个页面,其中显示了许多照片,每张照片都由 View 呈现PhotoListItemView。单击照片时,会出现一个模态视图,其中包含一组ModalAddToSetView列表SetListView。单击其中一个 Set 时SetView,我需要将photo_idand发送set_id到后端。

问题:在 Set 的点击处理程序中,我可以set_id通过使用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_id参数传递给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 回答