Collections - adrianlee44/ng-backbone GitHub Wiki

Collection

APP.factory('MyModels', ['NgBackboneCollection', 'MyModel', function (NgBackboneCollection, MyModel) {
  return NgBackboneCollection.extend({
    model: MyModel,
    url: '/my-models',

    constructor: function MyModels() {
      // ...

      NgBackboneCollection.apply(this, arguments);
    },
    
    initialize: function () {
      // ...

      // must be called when overriding initialize()
      NgBackboneCollection.prototype.initialize.apply(this, arguments);
    }
  });
}]);

Fetching

APP.controller('MyController', ['$scope', 'MyModels', function ($scope, MyModels) {
  // my_model_collection.constructor.class === 'MyModels'
  var my_model_collection = new MyModels();

  // $http promise
  my_model_collection.fetch({
    error: function (model, response, options) {},
    success: function (model, response, options) {}
  });

  // iterable, will be populated once the $http promise returns
  var my_models = my_model_collection.$models;
}]);

Status

Any event that is triggered on a model in a collection will also be triggered on the collection directly, for convenience. This allows you to listen for changes to specific attributes in any model in a collection. More Info.

This means that any actions on a model will trigger for it's associated collection.

Method Status Before onCreate onDelete onFetch onSave After
my_model.destroy() my_model_collection.$status.deleting false false true false false false
my_model_collection.fetch() my_model_collection.$status.loading false false false true false false
my_model.save() my_model_collection.create() my_model_collection.$status.saving false true false false true false
any method my_model_collection.$status.syncing false true true true true false

Events