Answer the question
In order to leave comments, you need to log in
Do I need to instantiate classes all the time in Backbone.js?
Hello,
in the code below, given a certain state of the router, a view is instantiated without being placed in a variable. A rather banal story happens in this view: it appends to the body of the div document, in which it shows the text of the news, after closing it deletes itself (this.$el.remove).
Pbl.classes.Router = Backbone.Router.extend({
routes: {
'' : 'index',
'news/add' : 'newsAdd',
'news/:id' : 'getNews',
},
getNews: function(id) {
new Pbl.classes.Views.NewsItem({
model: new Pbl.classes.Models.NewsItem(id)
});
}
});
Answer the question
In order to leave comments, you need to log in
There is no point in constantly creating and deleting views, you can just hide them with styles. This, by the way, will allow you to animate the show/hide.
I do not think that there is a fundamental difference, how many times to show the view, 1 time or 10. Today it is like this, but tomorrow the project has developed, the requirements have changed, and it has become that.
Hmm, they usually choose from a collection... And where to call this selection is a matter of organization. My routers are always stupid and just shoot events, and all the connecting code is in views (which are actually controllers, especially if you use two-way binding). But, probably, it is possible somehow differently, the main thing is to remember the principle of single responsibility.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question