PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/js/todos/src/collection/todos.js

http://github.com/pieter-vanderwerff/backbone-require-wire
JavaScript | 88 lines | 45 code | 23 blank | 20 comment | 3 complexity | 27d6ceb556741cd436bb110ba310af0b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. define( function( require ) {
  2. // Libraries
  3. var Backbone = require( 'backbone' ),
  4. _ = require( 'underscore' ),
  5. when = require( 'when' ),
  6. Store = require( 'backbone/localstorage' );
  7. // Todos Collection
  8. return Backbone.Collection.extend( {
  9. // Will be overwritten with an injected wire function
  10. _createTodo: function() {},
  11. // Override Backbone's default create method so we can take advantage of wire.js's creation methods
  12. // Creates our todo Model and View, then saves it and passes it to the collection
  13. create: function( attributes ) {
  14. var self = this;
  15. // Run the function created in our wiring spec, to create necessary objects
  16. // Pass in our attributes so they can be used in the creation of the model
  17. // BEWARE: Wire does magic here!
  18. return this._createTodo( { 'todo_attributes': attributes } ).then( function( todo_context ) {
  19. // After creating the model, add it to the collection
  20. self.add( todo_context.model );
  21. // When the model is destroyed kill the whole context
  22. todo_context.model.bind( 'destroy', function() {
  23. todo_context.destroy();
  24. } );
  25. } );
  26. },
  27. // Override Backbone's default reset method
  28. // Now we can use our new "Create" method when we load our local storage data
  29. reset: function( models, options ) {
  30. var self = this;
  31. models || (models = []);
  32. options || (options = {});
  33. // Empty collection
  34. this.each(this._removeReference);
  35. this._reset();
  36. // Create models
  37. var promises = _.map( models, function( model ) {
  38. return self.create( model );
  39. } );
  40. // When the models have finished creating, trigger the 'reset' event
  41. if ( !options.silent ) when.all( promises, function() {
  42. self.trigger( 'reset', self, options );
  43. } );
  44. return this;
  45. },
  46. // Save all of the todo items under the `"todos"` namespace.
  47. localStorage: new Store("todos-backbone-require-wire"),
  48. // Filter down the list of all todo items that are finished.
  49. done: function() {
  50. return this.filter( function( todo ) { return todo.get( 'done' ); } );
  51. },
  52. // Filter down the list to only todo items that are still not 'done'.
  53. remaining: function() {
  54. return this.without.apply( this, this.done() );
  55. },
  56. // Call destroy on all models that are 'done'
  57. clearDone: function() {
  58. _.invoke( this.done(), 'destroy' );
  59. },
  60. // Set all models to have the done value passed in
  61. toggleDone: function( done ) {
  62. this.invoke( 'save', { 'done': !!done } );
  63. }
  64. } );
  65. } );