简体   繁体   English

如何将参数传递给视图

[英]How to pass parameters to a view

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? 我怎样才能做到这一点?

ItemView = Backbone.View.extend({
    tagName: 'li',
    events: {
        'click': 'showMenu'
    },
    initialize: function() {
        _.bindAll(this, 'render');
    },
    render: function() {
    return $(this.el).html(this.model.get('name'));
    },
    showMenu: function() {
        var itemColl = new ItemColl();
        new MenuView({collection: itemColl}); // how to pass the position of menu here?
    }
});

You just need to pass the extra parameter when you construct the MenuView. 您只需在构造MenuView时传递额外参数。 No need to add the initialize function. 无需添加initialize函数。

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

And then, in MenuView , you can use this.options.position . 然后,在MenuView ,您可以使用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. 更新:由于@mu是太短的状态 ,因为1.1.0, Backbone Views不再自动附加传递给构造函数的选项作为this.options,但如果你愿意,你可以自己做。

So in your initialize method, you can save the options passed as this.options : 所以在你的initialize方法中,你可以保存作为this.options传递的options

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

or use some finer ways as described by @Brave Dave . 或者使用@Brave Dave描述的更精细的方法。

Add an options argument to initialize : 添加options参数以进行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 有关更多信息: http//backbonejs.org/#View-constructor

As of backbone 1.1.0, the options argument is no longer attached automatically to the view (see issue 2458 for discussion). 从主干1.1.0开始, options参数不再自动附加到视图(请参阅问题2458以供讨论)。 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
});

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, 添加一个options参数以在视图中初始化您正在获取传递的变量,

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});

Use this.options to retrieve argumentr in view 使用this.options在视图中检索argumentr

 // 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/ 工作示例: http//jsfiddle.net/Cpn3g/1771/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM