PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/Collection.js

https://github.com/Meenatchisundaram/gapvis
JavaScript | 36 lines | 27 code | 5 blank | 4 comment | 6 complexity | 28f42a0d75dfc3877afe7ab1e6655472 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Core setup for collections
  3. */
  4. define(function() {
  5. return Backbone.Collection.extend({
  6. // fetch list without overwriting existing objects (copied from fetch())
  7. fetchNew: function(options) {
  8. options = options || {};
  9. var collection = this,
  10. success = options.success;
  11. options.success = function(resp, status, xhr) {
  12. _(collection.parse(resp, xhr)).each(function(item) {
  13. if (!collection.get(item.id)) {
  14. collection.add(item, {silent:true});
  15. }
  16. });
  17. if (!options.silent) collection.trigger('reset', collection, options);
  18. if (success) success(collection, resp);
  19. };
  20. return (this.sync || Backbone.sync).call(this, 'read', this, options);
  21. },
  22. getOrCreate: function(modelId) {
  23. var model = this.get(modelId);
  24. if (!model) {
  25. model = new this.model({ id: modelId});
  26. this.add(model);
  27. }
  28. return model;
  29. }
  30. });
  31. });