Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (33 sloc) 1.03 KB

Backbone Store Collection

Extends a collection's get method to fetch models from the server if not found locally.

Write ups: Using Backbone.js Class Properties as Data Stores and Part 2

Usage

Posts = Backbone.StoreCollection.extend({
  url: '/posts'
});

var posts = new Posts(),
    post = new Backbone.Model({ id: 1 });
    
posts.add(post);

// Retrieving model from collection
posts.get(1, function() {
  success: function(model, response) {
    // do something with model
  }
});

// Fetching model from server
posts.get(32, function() {
  success: function(model, response) {
    // do something with model
  },
  error: function(model, response) {
    // say sorry
  }
});

Todo

Add tests.

Licence

Backbone Store Collection is copyright © 2012 Dom Christie and released under the MIT license.