vidigami / backbone-orm

A polystore ORM for Node.js and the browser
http://vidigami.github.io/backbone-orm
MIT License
238 stars 15 forks source link

Best way to have Backbone-Collections? #21

Closed petershaw closed 10 years ago

petershaw commented 10 years ago

Hi, i play around with some models and sqlite adapter that is working well and fits my needs so far. What i can't figure out is a good way to crate a collection of models with backbone-orm. Unfortunately i can not find an example, or webresource. (For a short moment I wonder if i am the only one who think that a Table, or resultset is a collection of modells).

How do you build up collections of models, that relates to backbone-orm? How can i become a collection back of a result by filtering a parameter?

Thanks a lot, ps

marcelklehr commented 10 years ago

I think you can just wrap the array of resulting models in a collection, no?

petershaw commented 10 years ago

Ahh, OK. This works for me:

module.exports  = function(Backbone, $){
    var AccountModel = require('./account')(Backbone);

    var accountModels = new AccountModel({isActive: 1});

    console.log((accountModels));

    var ActiveAccountCollection = Backbone.Collection.extend({
        model: AccountModel

    });
    ActiveAccountCollection.prototype.sync = SQLSync(ActiveAccountCollection);

    return ActiveAccountCollection;
}

But returns 4 Documents, 3 of 4 Docs are with isActive = 0. How do i filter right?

petershaw commented 10 years ago

Ok, after i understand how to find (sorry late) i solved this issue by my self :-)

For someone who came across, here is my solution:

module.exports  = function(Backbone, $){
    var AccountModel = require('./account')(Backbone, $);

    var accountModels = new AccountModel();

    var ActiveAccountCollection = Backbone.Collection.extend({

        fetch: function(callback){
            var that = this;

            AccountModel.find( 
                {isActive: true}
                , function(err, account){
                    if(err){ callback.error(err) };
                    _.each(account, function(account){
                        that.add(account);
                    });
                    callback.success();
                }
            );
        }
    });

    ActiveAccountCollection.prototype.sync = SQLSync(ActiveAccountCollection);

    return ActiveAccountCollection;
}

This gets a collection of all active acoounts.

Thanx a lot @marcelklehr

kmalakoff commented 10 years ago

Good to hear to figured it out.

The way to set up a collection is also found in the tests although it shows the standard fetch syntax using the query parameters on the url. Query parameters on the url would be a different way to implement what you are trying to do if you want to subclass to implement a special collection for active accounts, but we rarely subclass.

In general, with BackboneORM, we tend to use the Model query syntax and just reset them into a collection rather than using Backbone's fetch methods.

Backbone = require 'backbone'
Account = require './account'

active_accounts = new Backbone.Collection()
Account.find {isActive: true}, (err, models) -> active_accounts.reset(models)