PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/collections/pieces.js

https://bitbucket.org/jawa/chess
JavaScript | 43 lines | 34 code | 7 blank | 2 comment | 0 complexity | bcea2f2d181d4be0be1e91eac3b32b20 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. define(
  2. ['backbone', 'models/chessman', 'views/chessman'],
  3. function (Backbone, Chessman, ChessmanView) {
  4. var Pieces = Backbone.Collection.extend({
  5. model: Chessman,
  6. initialize: function () {
  7. this.on('add', function (piece) {
  8. var view = new ChessmanView({
  9. model: piece,
  10. className: piece.get('type') + ' chessman'
  11. });
  12. piece.view = view;
  13. });
  14. this.on('remove', function (piece) {
  15. piece.view.remove();
  16. });
  17. },
  18. onFields: function (fields) {
  19. return this.filter(function(piece) {
  20. return fields.indexOf(piece.get('position')) !== -1;
  21. });
  22. },
  23. removeAll: function () {
  24. // Workaround:
  25. // According to https://github.com/documentcloud/backbone/issues/263 you can't delete all model objects from a collection at once.
  26. var self = this;
  27. _(this.map(function(piece) {
  28. return piece.cid;
  29. })).each(function(cid) {
  30. self.remove(self.getByCid(cid));
  31. });
  32. }
  33. });
  34. return Pieces;
  35. }
  36. );