E
E
elailasou2015-03-25 13:26:46
JavaScript
elailasou, 2015-03-25 13:26:46

Please point out the flaws in the Backbone.js code?

I study Backbone.js, it comes with a creak. What can be improved in this simple example?
jsFiddle

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2015-03-25
Antonikhin @STJ

In fact, the example is normal. Backbone was created for that, so that you would do everything yourself and how you want.
Unless I advise a puppet, it will ease your suffering from backbone.

K
Konstantin Kitmanov, 2015-03-25
@k12th

render: function(person) {
        var that = this;
        this.$el.empty();
        this.collection.each(function(person) {
            var personView = new PersonView({model: person});
            that.$el.append(personView.render().el);
        });
        return this;
    }

Collection#each is just a curried _.each that takes the context for the iterator as its third argument. That is, you can rewrite it like this:
render: function(person) {
        this.$el.empty();
        this.collection.each(function(person) {
            var personView = new PersonView({model: person});
            this.$el.append(personView.render().el);
        }, this);
        return this;
    }

And this piece will slow down the more, the more elements will be in the list.
I usually don't do a view for each element, one view is enough for the entire list.
Well, the little things - indents, always use {}, decide whether double or single quotes, do not use single-letter variables and function parameters (what kind of e is error, exception, event?).
Otherwise, there is no crime :)

I
Ilya Ablamonov, 2015-04-02
@flamefork

PersonView.prototype.render is called 2 times: 1 time in the constructor (don't do this) and once in the collection's render loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question