Reputation: 2029
I'm try pass some parameters to view constructor.
I'm creating my object in this way: new HeadCell({isSortable: true, parent: this});
So in the HeadCell initialize:
initialize: function(options){
console.log("HeadCell initialized");
console.log(options);
console.log(options.isSortable);
}
The console output is:
HeadCell initialized
Object {isSortable: true, parent: r}Uncaught TypeError: Cannot read property 'isSortable' of undefined
Any ideas?
UPDATE 1
Im try run a little test in a fiddle, and the result is the same.
http://jsfiddle.net/ramiromd90/hHFP3/
UPDATE 2
I try your examples on fiddle, and works fine ! But, in my code not works.
Here a fiddle with a little example of my real code:
http://jsfiddle.net/ramiromd90/XCK47/
UPDATE 3
Thanks for all replies, the problem was that i dont pass the parameters in the loop (see the fiddle). The ways to pass parameters that us says are correct !. Thanks
Upvotes: 0
Views: 2707
Reputation: 4400
The code in your fiddle is incorrect because you're calling this.options
when the options parameter is never initialized as part of the view object. In other words, you're calling a property that doesn't exist.
Try this instead:
$(function(){
var Aview = Backbone.View.extend({
initialize: function(options){
console.log(options.foo);
console.log(options.bar);
}
});
new Aview({foo:'isFoo!',bar:'isBar!'});
});
Now, lets get into a bit more detail on what's actually happening to the options parameter when you pass it as an argument to the constructor.
According to the doc
There are several special options that, if passed, will be attached directly to the view: model, collection, el, id, className, tagName, attributes and events.
What does that mean? Well, the options parameter actually gets used in two ways in the view constructor.
First, it sets the special options using this call
_.extend(this, _.pick(options, viewOptions));
which returns a new object that contain only the special options in viewOptions
, and then adds those options to the view object.
Next, once these special options have been added, it then calls the initialize function.
this.initialize.apply(this, arguments);
In this case, it's equivalent to calling
currentViewObject.initialize({anOptionsAttribute: 'hello'});
Which allows you to access the options object either as a parameter, or through the arguments
object. As you can see, at no point is the options parameter set as a property, hence calling this.options
is just nonsensical.
Upvotes: 4
Reputation: 2232
View = Backbone.View.extend({
initialize: function(options){
console.log(options);
console.log(options.isSortable);
}
});
var view = new View({isSortable: true, parent: this });
I don't see why it's not working. Have you double checked your spelling?
"this.options" was removed in backbone 1.1.0 see:
http://backbonejs.org/#upgrading
In 1.1, Backbone Views no longer have the options argument attached as this.options automatically. Feel free to continue attaching it if you like.
Upvotes: 1