1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * `cr-settings-pref-tracker` is a utility element used to track the
8 * initialization of a specified preference and throw an error if the pref
9 * is not defined after prefs have all been fetched.
13 * <cr-settings-pref-tracker pref="{{prefs.settings.foo.bar}}">
14 * </cr-settings-pref-tracker>
16 * @element cr-settings-pref-tracker
21 * An array of all the tracker instances.
22 * @type {!Array<!CrSettingsPrefTrackerElement>}
27 * Validates all tracker instances.
30 var validateAll_ = function() {
31 instances
.forEach(function(tracker
) {
36 document
.addEventListener(CrSettingsPrefs
.INITIALIZED
, validateAll_
);
38 Polymer('cr-settings-pref-tracker', {
41 * The Preference object being tracked.
62 * Throws an error if prefs are initialized and the tracked pref is not
66 validate_: function() {
67 this.async(function() {
68 // Note that null == undefined.
69 if (CrSettingsPrefs
.isInitialized
&& this.pref
== null) {
70 // HACK ALERT: This is the best clue we have as to the pref key for
71 // this tracker. This value should not be relied upon anywhere or
72 // actually used besides for this error message.
74 var parentPrefString
= this.parentNode
&& this.parentNode
.host
&&
75 this.parentNode
.host
.getAttribute('pref');
76 if (parentPrefString
) {
77 keyHint
= parentPrefString
.match(/{{([a-z._]+)}}/)[1];
80 throw new Error('Pref not found. Key Hint: ' + keyHint
);