OLD | NEW |
(Empty) | |
| 1 |
| 2 (function(scope) { |
| 3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; |
| 4 MoreRouting.Params = Params; |
| 5 |
| 6 /** |
| 7 * A collection of route parameters and their values, with nofications. |
| 8 * |
| 9 * Params prefixed by `__` are reserved. |
| 10 * |
| 11 * @param {!Array<string>} params The keys of the params being managed. |
| 12 * @param {Params=} parent A parent route's params to inherit. |
| 13 * @return {Params} |
| 14 */ |
| 15 function Params(params, parent) { |
| 16 var model = Object.create(parent || Params.prototype); |
| 17 // We have a different set of listeners at every level of the hierarchy. |
| 18 Object.defineProperty(model, '__listeners', {value: []}); |
| 19 |
| 20 // We keep all state enclosed within this closure so that inheritance stays |
| 21 // relatively straightfoward. |
| 22 var state = {}; |
| 23 _compile(model, params, state); |
| 24 |
| 25 return model; |
| 26 } |
| 27 Params.prototype = Object.create(MoreRouting.Emitter); |
| 28 |
| 29 // Utility |
| 30 |
| 31 function _compile(model, params, state) { |
| 32 params.forEach(function(param) { |
| 33 Object.defineProperty(model, param, { |
| 34 get: function() { |
| 35 return state[param]; |
| 36 }, |
| 37 set: function(value) { |
| 38 if (state[param] === value) return; |
| 39 state[param] = value; |
| 40 model.__notify(param, value); |
| 41 }, |
| 42 }); |
| 43 }); |
| 44 } |
| 45 |
| 46 })(window); |
OLD | NEW |