group.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. Group = function(router, options, parent) {
  2. options = options || {};
  3. if (options.prefix && !/^\/.*/.test(options.prefix)) {
  4. var message = "group's prefix must start with '/'";
  5. throw new Error(message);
  6. }
  7. this._router = router;
  8. this.prefix = options.prefix || '';
  9. this.name = options.name;
  10. this.options = options;
  11. this._triggersEnter = options.triggersEnter || [];
  12. this._triggersExit = options.triggersExit || [];
  13. this._subscriptions = options.subscriptions || Function.prototype;
  14. this.parent = parent;
  15. if (this.parent) {
  16. this.prefix = parent.prefix + this.prefix;
  17. this._triggersEnter = parent._triggersEnter.concat(this._triggersEnter);
  18. this._triggersExit = this._triggersExit.concat(parent._triggersExit);
  19. }
  20. };
  21. Group.prototype.route = function(pathDef, options, group) {
  22. options = options || {};
  23. if (!/^\/.*/.test(pathDef)) {
  24. var message = "route's path must start with '/'";
  25. throw new Error(message);
  26. }
  27. group = group || this;
  28. pathDef = this.prefix + pathDef;
  29. var triggersEnter = options.triggersEnter || [];
  30. options.triggersEnter = this._triggersEnter.concat(triggersEnter);
  31. var triggersExit = options.triggersExit || [];
  32. options.triggersExit = triggersExit.concat(this._triggersExit);
  33. return this._router.route(pathDef, options, group);
  34. };
  35. Group.prototype.group = function(options) {
  36. return new Group(this._router, options, this);
  37. };
  38. Group.prototype.callSubscriptions = function(current) {
  39. if (this.parent) {
  40. this.parent.callSubscriptions(current);
  41. }
  42. this._subscriptions.call(current.route, current.params, current.queryParams);
  43. };