

Backbone.js Collection - PatriciaLopes
http://mrbool.com/backbone-js-collection/27648

======
eranation
Another convention I'm missing is capital for "Classes" the model is a model
class, not an instance

    
    
        var imageModel = Backbone.Model.extend...
    

Should be

    
    
        var ImageModel = Backbone.Model.extend...
    

Another thing, adding to the collection using add is not the best practice
IMHO,

    
    
        imageGallery.add(images);
    

As far as I know it will trigger the add event for each model being added, I
prefer adding multiple models via the reset method

Don't want to criticize the OP or something (I've read mostly great posts from
mrbool...) but if anyone new to Backbone intending to learn from it, I suggest
to take it with a grain of salt (and as other's stated, it's not using the
latest backbone version - 1.0)

There are many great free resources for learning Backbone.js (backbonejs.org
is one of the best, but not always easy to get started from there)

But I found that spending a few dollars on paid courses is sometimes worth it.
I'm not affiliated, but for me there were 3 that I found well worth their cost
for learning backbone quickly (using relatively "latest" best practices, as
much as latest is a moving target)

1) code school's (part 1 and part 2) - simple but practical

2) tuts plus (also 2 parts, both are great), 2nd part has some free videos on
nested collections, must watch

3) peep code (3 parts) - the only that I haven't used personally, but it got
great reviews

I think backbone is here to stay, Angular / Ember might be nice for a new
project, but if you want to refactor without rewriting your code from scratch
the Angular or the Ember way, backbone is simply great for the job.

~~~
azamuddin
Hey eranation, for multiple words we can use CamelCase and avoid underscore.
And when we use multiple words, both 'imageModel' and 'ImageModel' are good
practice, see here <http://ricostacruz.com/backbone-
patterns/#naming_convention>

and about adding multiple things to collection the reason for using 'add'
instead of 'reset' is, 'add' will maintain the existing data, while reset will
clear the existing data then replace it.

I like the code schools's part 1 and 2 which was written by Jeremy Ashkenas,
it is simple but practical, I agree.

~~~
eranation
I have to disagree, the pattern for classes is clearly starting with an
uppercase letter, and for instances with a lower case letter. In the example
we have a model class, not a model instance.

> Classes often start in uppercase letters, while instances start with
> lowercase letters.

------
danso
Because the OP doesn't mention which version of Backbone he's using nor is
there a date for his posting, it's hard to tell how up to date the advice is.
This stood out to me (though I think it's relatively minor):

    
    
        Listing 7: Get model from collection by client id
              ImageGallery.getByCid(534);
    

The `getByCid` method has been removed since 0.9.10:
[https://github.com/documentcloud/backbone/compare/0.9.9...0....](https://github.com/documentcloud/backbone/compare/0.9.9...0.9.10)

~~~
wpgreenway
Also worth noting that his explanation of Collection.get: "Collection.get()
which accept single index of model as parameter like follows..." is incorrect
and should be Collection.at.

~~~
film42
That, or changed to, "Collection.get() which accept single _id_ of model as
parameter like follows..."

