How to pass parameters to a view

前端 未结 5 1350
梦谈多话
梦谈多话 2020-12-04 09:22

I have a series of buttons which when clicked display a popup menu positioned just below the button. I want to pass the position of button to the view. How can I do that?

5条回答
  •  时光说笑
    2020-12-04 10:00

    As of backbone 1.1.0, the options argument is no longer attached automatically to the view (see issue 2458 for discussion). You now need to attach the options of each view manually:

    MenuView = Backbone.View.extend({
        initialize: function(options) {
            _.extend(this, _.pick(options, "position", ...));
        }
    });
    
    new MenuView({
        collection: itemColl,
        position: this.getPosition(),
        ...
    });
    

    Alternatively you can use this mini plugin to auto-attach white-listed options, like so:

    MenuView = Backbone.View.extend({
        options : ["position", ...] // options.position will be copied to this.position
    });
    

提交回复
热议问题