Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?     } }); 
like image 835
vikmalhotra Avatar asked Oct 18 '11 06:10

vikmalhotra


People also ask

Can I pass parameters to a view?

No, in SQL Server, we cannot pass parameters to a view. And it can be considered as one main limitation of using a view in SQL Server. Moreover, even if we try to pass parameters to a view, the SQL Server will return an error.

Can we pass parameter to view in Oracle?

You can't pass a parameter to a view.

Can we pass parameter to view in mysql?

In the view use a cross join to the parameter table and put WHERE param_table. connection_id = CONNECTION_ID() . This will cross join with only one row from the parameter table which is what you want. You can then use the other columns in the where clause for example where orders.

Can I declare variable in view?

You can't declare variables in a view.


2 Answers

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.

like image 55
dira Avatar answered Sep 20 '22 10:09

dira


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

like image 30
mu is too short Avatar answered Sep 22 '22 10:09

mu is too short