PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Oro/Bundle/FormBundle/Resources/public/js/multiple-entity/collection.js

https://github.com/diimpp/platform
JavaScript | 31 lines | 21 code | 3 blank | 7 comment | 2 complexity | fdb0138bfe6707f0631395b5733e0963 MD5 | raw file
  1. /*global define*/
  2. define(['backbone', './model'
  3. ], function (Backbone, EntityModel) {
  4. 'use strict';
  5. /**
  6. * @export oroform/js/multiple-entity/collection
  7. * @class oroform.MultipleEntity.Collection
  8. * @extends Backbone.Collection
  9. */
  10. return Backbone.Collection.extend({
  11. model: EntityModel,
  12. initialize: function () {
  13. this.on('change:isDefault', this.onIsDefaultChange, this);
  14. },
  15. onIsDefaultChange: function (item) {
  16. // Only 1 item allowed to be default
  17. if (item.get('isDefault')) {
  18. var defaultItems = this.where({isDefault: true});
  19. _.each(defaultItems, function (defaultItem) {
  20. if (defaultItem.get('id') !== item.get('id')) {
  21. defaultItem.set('isDefault', false);
  22. }
  23. });
  24. this.trigger('defaultChange', item);
  25. }
  26. }
  27. });
  28. });