PageRenderTime 62ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/jquery-mobile/1.3.2/demos/examples/backbone-require/js/collections/CategoriesCollection.js

https://gitlab.com/0072016/Polymer-cdnjs-
JavaScript | 98 lines | 38 code | 39 blank | 21 comment | 0 complexity | d61298d49689f3316ab18be2ef016583 MD5 | raw file
  1. // Category Collection
  2. // ===================
  3. // Includes file dependencies
  4. define([ "jquery","backbone","models/CategoryModel" ], function( $, Backbone, CategoryModel ) {
  5. // Extends Backbone.Router
  6. var Collection = Backbone.Collection.extend( {
  7. // The Collection constructor
  8. initialize: function( models, options ) {
  9. // Sets the type instance property (ie. animals)
  10. this.type = options.type;
  11. },
  12. // Sets the Collection model property to be a Category Model
  13. model: CategoryModel,
  14. // Sample JSON data that in a real app will most likely come from a REST web service
  15. jsonArray: [
  16. { "category": "animals", "type": "Pets" },
  17. { "category": "animals", "type": "Farm Animals" },
  18. { "category": "animals", "type": "Wild Animals" },
  19. { "category": "colors", "type": "Blue" },
  20. { "category": "colors", "type": "Green" },
  21. { "category": "colors", "type": "Orange" },
  22. { "category": "colors", "type": "Purple" },
  23. { "category": "colors", "type": "Red" },
  24. { "category": "colors", "type": "Yellow" },
  25. { "category": "colors", "type": "Violet" },
  26. { "category": "vehicles", "type": "Cars" },
  27. { "category": "vehicles", "type": "Planes" },
  28. { "category": "vehicles", "type": "Construction" }
  29. ],
  30. // Overriding the Backbone.sync method (the Backbone.fetch method calls the sync method when trying to fetch data)
  31. sync: function( method, model, options ) {
  32. // Local Variables
  33. // ===============
  34. // Instantiates an empty array
  35. var categories = [],
  36. // Stores the this context in the self variable
  37. self = this,
  38. // Creates a jQuery Deferred Object
  39. deferred = $.Deferred();
  40. // Uses a setTimeout to mimic a real world application that retrieves data asynchronously
  41. setTimeout( function() {
  42. // Filters the above sample JSON data to return an array of only the correct category type
  43. categories = _.filter( self.jsonArray, function( row ) {
  44. return row.category === self.type;
  45. } );
  46. // Calls the options.success method and passes an array of objects (Internally saves these objects as models to the current collection)
  47. options.success( categories );
  48. // Triggers the custom `added` method (which the Category View listens for)
  49. self.trigger( "added" );
  50. // Resolves the deferred object (this triggers the changePage method inside of the Category Router)
  51. deferred.resolve();
  52. }, 1000);
  53. // Returns the deferred object
  54. return deferred;
  55. }
  56. } );
  57. // Returns the Model class
  58. return Collection;
  59. } );