如何将参数传递给视图

发布于 2024-12-10 11:49:40 字数 510 浏览 0 评论 0原文

我有一系列按钮,单击这些按钮时会显示位于按钮正下方的弹出菜单。我想将按钮的位置传递给视图。我怎样才能做到这一点?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

饮惑 2024-12-17 11:49:40

您只需在构造 MenuView 时传递额外的参数即可。无需添加initialize函数。

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

然后,在 MenuView 中,您可以使用 this.options.position

更新:@mu状态太短,自 1.1.0 起,主干视图不再自动附加作为 this.options 传递给构造函数的选项,但如果您愿意,您可以自己执行此操作。

因此,在您的 initialize 方法中,您可以可以将传递的 options 保存为 this.options

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

或者使用一些更好的方法,如 描述的作者:@Brave Dave

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.

吻风 2024-12-17 11:49:40

将选项参数添加到 initialize

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

然后在创建视图时传入一些选项:

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

有关详细信息:http ://backbonejs.org/#View-constructor

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

红焚 2024-12-17 11:49:40

从主干 1.1.0 开始,options 参数不再自动附加到视图(请参阅问题 2458 供讨论)。您现在需要手动附加每个视图的选项:

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

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

或者您可以使用这个迷你插件来自动附加白名单选项,像这样:

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

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
});
遇见了你 2024-12-17 11:49:40

从其他位置传递

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

添加一个选项参数来初始化,以查看您正在获取传递的变量,

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

以获取值
使用 -

   var v = new ItemView({ pos: this.options.positions});

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});
遥远的绿洲 2024-12-17 11:49:40

使用 this.options 在视图中检索参数

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

工作示例:http://jsfiddle.net /Cpn3g/1771/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文