/js/Node.js

https://bitbucket.org/juliand89/firewall-game · JavaScript · 39 lines · 27 code · 5 blank · 7 comment · 1 complexity · daee68f6becae45ada5b13f123a33c2a MD5 · raw file

  1. var Node = Backbone.Model.extend({
  2. defaults: function () {
  3. return {
  4. 'name': '',
  5. 'ip': '',
  6. // the node ID is what ties together nodes in a campaign's node
  7. // list and graph.
  8. 'id': Node.getID(),
  9. 'rules': new RuleList()
  10. };
  11. },
  12. clone: function () {
  13. var clone = Backbone.Model.prototype.clone.call(this);
  14. clone.id = Node.getID();
  15. // we also need to set the id as an attribute so that it's serialized
  16. // on export and not ignored.
  17. clone.set('id', clone.id);
  18. return clone;
  19. },
  20. initialize: function (attributes) {
  21. if (!(this.get('rules') instanceof RuleList)) {
  22. this.set('rules', new RuleList(attributes.rules, {}));
  23. }
  24. }
  25. });
  26. Node.getID = function () {
  27. // Math.random() is actually a POOR choice for generating unique IDs. But
  28. // for right now, it's easy. The random number is converted to hex for
  29. // aesthetics.
  30. return Math.random().toString(16).substr(2)
  31. };
  32. Nodes = Backbone.Collection.extend({
  33. model: Node
  34. });