Backbone.js - passing arguments through constructors - javascript

Scenario:
I got an alert() saying undefined when I (try to) set the myVar variable through the Constructor. However if I uncomment the myVar that's living inside the myView, the alert then instead says "Hello from inside", just as one would expect.
Question:
Does this mean that I cannot set any params in the constructor of the view except backbones own params, such as model, collection, el, id, className & tagName?
Manual: http://documentcloud.github.com/backbone/#View-constructor
The code:
var myView = Backbone.View.extend({
//myVar : 'Hello from inside',
initialize: function() {
alert(this.myVar);
}
)};
new myView({myVar: 'Hello from outside'});

Options passed into the constructor are automatically stored as this.options
var myView = Backbone.View.extend({
myVar : 'Hello from inside',
initialize: function() {
alert(this.options.myVar);
}
)};
new myView({myVar: 'Hello from outside'});

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:
MyView = Backbone.View.extend({
initialize: function(options) {
_.extend(this, _.pick(options, "myVar", ...));
// options.myVar is now at this.myVar
}
});
new MyView({
myVar: "Hello from outside"
...
});
Alternatively you can use this mini plugin to auto-attach white-listed options, like so:
MyView = BaseView.extend({
options : ["myVar", ...] // options.myVar will be copied to this.myVar on initialize
});

Related

How to override existing Backbone Model method keeping other methods same

I have a backbone model which I don't have access to edit.So i want to modify the method of it, so that other methods functionality wont affect.
ShoppingDetail = Backbone.Model.extend({
className: 'CartID',
fetch: function() {},
checkForChanges: function() {},
newCoupon: function() {},
saveAndallow: function() {}
});
shoppingDetailModel = new ShoppingDetail();
shoppingCartView = new ShoppingCartView({
model: shoppingDetailModel
});
So i want to override saveAndallow method of the model.How could i do that without affecting other methods of that model
Why don't you create a new Model which will extend your ShoppingDetail Model ?
ShoppingDetail = Backbone.Model.extend({
className: 'CartID',
fetch: function() {},
checkForChanges : function() {},
newCoupon: function(){},
saveAndallow: function(){}
});
NewShoppingDetail = ShoppingDetail.extend({
saveAndallow: function(){};
});
shoppingDetailModel = new NewShoppingDetail();
shoppingCartView = new ShoppingCartView({
model : shoppingDetailModel
});
So now, when you call any method, it will first check if it is available in NewShoppingDetail, if not available, then it will check ShoppingDetail.

My Backbone.View won't render

I reorganized my fully-working Backbone.js project and moved all my models, collections and views into separate files, and did a little rewriting, and now it won't render. I've tried everything I can think of. Any tips?
var SessionView = Backbone.View.extend({
model: Session,
el: '#list',
template: _.template($('#session-template').html()),
initialize: function () {
this.model.bind('change', _.bind(this.render, this));
this.render();
},
render: function () {
this.$el.html(this.template({sessions: sessionList}));
return this;
}
});
var sessionView = new SessionView();
var SessionListView = Backbone.View.extend({
el: '#list',
model: sessionList,
initialize: function () {
sessionList.bind('add', this.add, this);
sessionList.bind('reset', this.add, this);
sessionList.fetch();
},
render: function () {
var view = new sessionListView();
this.$el.append(view.render().el);
new SessionView({model: Session});
return this;
}
});
var sessionListView = new SessionListView();
Few things that I noticed:
Backbone.View does not have a model property. Only Backbone.Collection has a model property, which backbone will use to create an instance of model using the specified model constructor (blueprint) and data passed to it.
But views doesn't have a functionality like this (as far as I know). People usually pass an instance of a specific type of model with the options while creating a view, that's not the same as specifying a model property which points to a model constructor in the View's constructor.
sessionList doesn't seems to be an instance of a model (since it is specified in the view's constructor. If it's an instance it'll be shared by all the SessionListView instances which is not the desired behavior in most cases) and seems to be undefined
in the following:new SessionView({model: Session}); Session doesn't look like an instance of a model (Doesn't start with a capital letter, hoping you're following naming conventions) and also seems to be undefined
Well nothing is stopping you from specifying a model constructor in view's constructor or passing a model constructor into the view, but then you should make an instance of it (mostly while initializing) inside the view to work with. In other words you can not do blueprintOfAModel.bind('change'..); and you should build an actual model for the view to work with.
You seems to be creating new SessionListView in the render method of SessionListView itself with var view = new sessionListView(); won't that create infinite number of SessionListView instances when you simply try to create one..?
Well by looking at it again, you are not calling the actual constructor SessionListView with the new operator, but with an instance of it (sessionListView) which is likely to throw an error.
Both SessionView and SessionListView points to the same element, which seems weird. I haven't seen people doing that before since modifying the el of one view will have an impact on the other view, which is not desired in most practical cases.
Also judging by the names, since you have a list view of session, SessionView should not be pointing to a particular element with an id selector. You should create a new element for each SessionView instance. Backbone will do that for you if you don't specify el property.
(I'd say you created an unintentional mess that wasn't there with a little rewrite :)
To make sense, your code should look somewhat like the following. Note that things starting with capital letter are constructor functions and things starting with small letters are object instances
var Session = Backbone.Model.extend({
defaults: {},
initialize: function() {}
});
var SessionList = Backbone.Collection.extend({
model: Session,
initialize: function() {}
});
var SessionView = Backbone.View.extend({
initialize: function() {
this.model.bind('change', _.bind(this.render, this));
this.render();
},
template: _.template($('#session-template').html()),
render: function() {
this.$el.html(this.template({
session: this.model
}));
return this;
}
});
var SessionListView = Backbone.View.extend({
el: '#list',
initialize: function() {
this.collection.bind('add', this.add, this); // method doesn't exist, should throw error
this.collection.bind('reset', this.add, this); // same here
this.collection.fetch(); // <--- watch out, this happens asynchronously
},
render: function() {
// iterate through collection, create instances of SessionView and append to el
return this;
}
});
var sessionList = new SessionList(); // will create n number of Session instances in future
var sessionListView = new SessionListView({ // will create n number of SessionView instances in future
collection: sessionList
});

Backbone extend broken

This has never previously happened and its leaving me a little miffed... But if I create a view and try to extend it extend isn't a function on a on appears like a valid instance of the class.
var gv = Backbone.View.extend({
//Stuff here
});
console.log(gv);
//child {cid: "view2", $el: jQuery.fn.init[1], el: div.shell, constructor: function, events: Object…}
gv.extend({
//Stuff here
});
//Uncaught TypeError: gv.extend is not a function
I have added a working example.
$(document).ready(function() {
var RN = {};
RN.gvCreator = Backbone.View.extend({
el: '.shell',
render: function() {
console.info('building stuff');
}
});
//set up the global view for all menu items etc
RN.gv = new RN.gvCreator();
RN.gv.render();
console.info(RN.gv);
var indexView = RN.gv.extend({
el: '.content',
render: function() {
console.info('working');
}
});
Backbone.history.start();
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<div class="shell"></div>
In your example, you reassign to RN.gv. At first it is a constructor, but then you clobber it with an instance.
Generally speaking, view instances cannot be extended after creation (at least not with that method).
You can solve your problem by having two separate variables for your constructor and instance. Just note that (re-)extending the constructor after instances have already been created will only affect future view instances- they will not retroactively affect previously created view instances.
Edit: In response to the comment below, this is how the new view extend should be done:
var indexView = RN.gvCreator.extend({
el: '.content',
render: function() {
console.info('working');
}
});
This will not affect the properties of RN.gv, since that was already created with the RN.gvCreator constructor. (Note that RN.gvCreator is not modified by the extend statement above-- instead, a new view constructor is created which uses RN.gvCreator as its base.)
Try extending the prototype object of your view using underscore:
var View = Backbone.View.extend({
foo: 1
});
console.log(View);
_.extend(View.prototype, {
bar: 2
});
var aView = new View();
console.log(aView.foo);
console.log(aView.bar);

Passing parameters to a Backbone View (id, className and so on)

I have some code that makes a Backbone.View, based on some params I pass to it, like so:
// The Form View
var FormView = Backbone.View.extend({
initialize: function (opts) {
debugger; // This is here only to figure out what gets executed first: if "id" method or "initialize"
this.options = {};
this.options.id = opts.id;
this.options.className = opts.class;
},
id: function () {
debugger; // The application will stop here before the debugger I set in the initialize method
return this.options.id; // options is undefined!
},
className: function () {
return this.options.className; // options is undefined!
}
});
// The params
var params =
fid: "some-form",
class: "form-horizontal"
};
var myForm = new FormView(params);
But the this.options property is always undefined. As I can see, the method that sets the view's properties runs BEFORE the initialize method. As a workaround I think I could access the initialize method inside de id callback and call its arguments, but I'm not sure how to do this properly. And I don't think this is a good approach either.
Any ideas? - Thanks in advance.
Why don't you use the usual way of passing options to the view? Something like:
var FormView = Backbone.View.extend({
initialize: function () {
this.foo = this.options.foo;
this.bar = this.options.bar;
}
});
var params = {foo: '1', bar: '2'};
var v = new FormView(params);
You can also pass as params {id: '1', className: 'your-class'} and Backbone will apply it automatically to the respective id and className properties of the target View.
You don't seem to be initializing the options attribute properly. You should have
initialize: function (opts) {
this.options = {}; // you were missing this part
this.options.id = opts.id;
this.options.className = opts.class;
},

Fetch data having a specific id defined in the view instance

I need to fetch data having a specific id
and which id is defined in the view instance.
Here the example, see the comments in MyModel definition:
// my view instance
var myView = new MyView({
model: {id: 12321}
});
MyView = Backbone.View.extends({
initialize: function()
{
myModel.fetch();
}
});
MyModel = Backbone.Model.extends({
url: function url ()
{
// how to get the id passed to view instance?
return "http:..../id/" + this.id;
}
});
Model should not has any knowledge of the existence of the View, so the View should be the one that sais to the Model which id to fetch:
MyView = Backbone.View.extends({
initialize: function()
{
myModel.id = this.model.id;
myModel.fetch();
}
});
(I've used your example code as template for my example, but I have to say I feel several weird things on it, I suppose is just a matter of taste)
Update: My very personal taste opinions
Is very difficult to do this but as you requested I'll share with you my very personal code review of your example code. Take this as it is: a very humble opinion.
this.model confused
I would not use attribute names that can create confussion:
var myView = new MyView({
model: {id: 12321}
});
Into this instance this.model is making reference to a raw Hash but in a Backbone context this is against the intuitive feeling that this is gonna be a Backbone.Model.
I rather change it for something like this:
var MyView = Backbone.View.extend({
initialize: function( opts ){
this.model_id = opts.model_id;
}
})
var myView = new MyView({ model_id: 12321 });
I think this naming is more intuitive.
close variables scopes
This code can only works if myModel is in an scope bigger that it should be:
MyView = Backbone.View.extends({
initialize: function()
{
myModel.fetch();
}
});
I rather prefer using more encapsulated scopes, even if myModel has been declared in the out-side context of your View the View should use a variable of its private context. For example
var MyView = Backbone.View.extends({
initialize: function( opts ) {
this.model = opts.model;
this.model.fetch();
}
});
var myView = new MyView({ model: myModel });
Check the detail that I have also added var in front of MyView because if not MyView will be a window global variable.
use the Backbone urlRoot
In your example, this ...
MyModel = Backbone.Model.extends({
url: function url ()
{
// how to get the id passed to view instance?
return "http:..../id/" + this.id;
}
});
... can be summarized as this:
MyModel = Backbone.Model.extends({
urlRoot: "http:..../id"
});

Categories

Resources