R
R
RaulDuke2015-10-19 16:40:38
JavaScript
RaulDuke, 2015-10-19 16:40:38

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

Please tell me:
1. Will this approach lead to a memory leak?
2. What is the best practice in case if you just need to show the model in the view, without reusing it.
3. Is it correct to select by id in the model (new Pbl.classes.Models.NewsItem(id)) or should it be done in the router/view?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-10-19
@RaulDuke

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 question

Ask a Question

731 491 924 answers to any question