OLD | NEW |
| (Empty) |
1 <!-- | |
2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | |
3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
6 Code distributed by Google as part of the polymer project is also | |
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
8 --> | |
9 <link rel="import" href="emitter.html"> | |
10 | |
11 <script> | |
12 (function(scope) { | |
13 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; | |
14 MoreRouting.Params = Params; | |
15 | |
16 /** | |
17 * A collection of route parameters and their values, with nofications. | |
18 * | |
19 * Params prefixed by `__` are reserved. | |
20 * | |
21 * @param {!Array<string>} params The keys of the params being managed. | |
22 * @param {Params=} parent A parent route's params to inherit. | |
23 * @return {Params} | |
24 */ | |
25 function Params(params, parent) { | |
26 var model = Object.create(parent || Params.prototype); | |
27 // We have a different set of listeners at every level of the hierarchy. | |
28 Object.defineProperty(model, '__listeners', {value: []}); | |
29 | |
30 // We keep all state enclosed within this closure so that inheritance stays | |
31 // relatively straightfoward. | |
32 var state = {}; | |
33 _compile(model, params, state); | |
34 | |
35 return model; | |
36 } | |
37 Params.prototype = Object.create(MoreRouting.Emitter); | |
38 | |
39 // Utility | |
40 | |
41 function _compile(model, params, state) { | |
42 params.forEach(function(param) { | |
43 Object.defineProperty(model, param, { | |
44 get: function() { | |
45 return state[param]; | |
46 }, | |
47 set: function(value) { | |
48 if (state[param] === value) return; | |
49 state[param] = value; | |
50 model.__notify(param, value); | |
51 }, | |
52 }); | |
53 }); | |
54 } | |
55 | |
56 })(window); | |
57 </script> | |
OLD | NEW |