How to pass parameters to a view

前端 未结 5 1340
梦谈多话
梦谈多话 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 09:58

    Use this.options to retrieve argumentr in view

     // Place holder
     <div class="contentName"></div>
    
     var showNameView = Backbone.View.extend({
            el:'.contentName',
            initialize: function(){
                // Get name value by this.options.name
                this.render(this.options.name);
            },
            render: function(name){
                $('.contentName').html(name);
            }
        });
    
        $(document).ready(function(){
            // Passing name as argument to view
            var myName1 = new showNameView({name: 'Nishant'});
        });
    

    Working Example: http://jsfiddle.net/Cpn3g/1771/

    0 讨论(0)
  • 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
    });
    
    0 讨论(0)
  • 2020-12-04 10:09

    Add an options argument to initialize:

    initialize: function(options) {
        // Deal with default options and then look at options.pos
        // ...
    },
    

    And then pass in some options when you create your view:

    var v = new ItemView({ pos: whatever_it_is});
    

    For more information: http://backbonejs.org/#View-constructor

    0 讨论(0)
  • 2020-12-04 10:10

    You just need to pass the extra parameter when you construct the MenuView. No need to add the initialize function.

    new MenuView({
      collection: itemColl,
      position: this.getPosition()
    })
    

    And then, in MenuView, you can use this.options.position.

    UPDATE: As @mu is too short states, since 1.1.0, Backbone Views no longer automatically attach options passed to the constructor as this.options, but you can do it yourself if you prefer.

    So in your initialize method, you can save the options passed as this.options:

    initialize: function(options) {
        this.options = options;
        _.bindAll(this, 'render');
    },
    

    or use some finer ways as described by @Brave Dave.

    0 讨论(0)
  • 2020-12-04 10:14

    pass from other location

     new MenuView({
       collection: itemColl,
       position: this.getPosition()
    })
    

    Add an options argument to initialize in view you are getting that passed variable,

    initialize: function(options) {
       // Deal with default options and then look at options.pos
       // ...
    },
    

    to get the value use -

       var v = new ItemView({ pos: this.options.positions});
    
    0 讨论(0)
提交回复
热议问题