Skip to content

Commit c132c22

Browse files
committed
fixed scope of models in client side javascript
1 parent 1731b42 commit c132c22

3 files changed

Lines changed: 29 additions & 26 deletions

File tree

services/language_translation/v2.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
var first = true;
189189

190190
langs.forEach(function(language){
191-
txt += (LANGUAGES[language] ? LANGUAGES[language] : lang);
191+
txt += (LANGUAGES[language] ? LANGUAGES[language] : language);
192192
if (first) {
193193
txt += ' - ';
194194
first = false;
@@ -307,7 +307,7 @@
307307
return a.domain;
308308
});
309309
}
310-
if (domains) {
310+
if (domains && domains.length) {
311311
$('select#node-input-domain').empty();
312312
var unique_domains = domains.filter(checkUnique);
313313

services/speech_to_text/v1.html

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@
8989
</script>
9090

9191
<script type="text/javascript">
92-
var models = null;
93-
var languages = null;
94-
var bands = null;
9592

96-
var language_selected = '';
97-
var band_selected = '';
93+
// Need to simulate a namespace, as some of the variables had started to leak across nodes
94+
function STT () {
95+
var models = null;
96+
}
97+
98+
// This is the namespace for stt. Currently only contains models, but more vars and functions may need to be
99+
// moved in if there is a clash with other nodes.
100+
var stt = new STT();
98101

99102
var LANGUAGES = { 'en-US' : 'US English',
100103
'pt-BR': 'Portuguese Braziilian',
@@ -105,6 +108,12 @@
105108
'ja-JP': 'Japanese'
106109
};
107110

111+
var languages = null;
112+
var bands = null;
113+
114+
var language_selected = '';
115+
var band_selected = '';
116+
108117

109118
// sorting functions
110119
function onlyUnique(value, index, self) {
@@ -114,7 +123,7 @@
114123
// Function to be used at the start, as don't want to expose any fields, unless the models are
115124
// available. The models can only be fetched if the credentials are available.
116125
function hideEverything() {
117-
if (!models) {
126+
if (!stt.models) {
118127
$('#credentials-not-found').show();
119128
$('label#node-label-message').parent().hide();
120129
$('input#node-input-continuous').parent().hide();
@@ -126,7 +135,7 @@
126135
// Check if there is a model then can show the fields.
127136
// available. The models can only be fetched if the credentials are available.
128137
function visibilityCheck() {
129-
if (models) {
138+
if (stt.models) {
130139
$('label#node-label-message').parent().hide();
131140
$('input#node-input-continuous').parent().show();
132141
$('select#node-input-lang').parent().show();
@@ -141,22 +150,22 @@
141150

142151

143152
// Simple check that is only invoked if the service is not bound into bluemix. In this case the
144-
// user has to provide credentials. Once there are credentials, then the models are retrieved.
153+
// user has to provide credentials. Once there are credentials, then the stt.models are retrieved.
145154
function checkCredentials() {
146155
var u = $('#node-input-username').val();
147156
var p = $('#node-input-password').val();
148157

149158
if (u && u.length && p) {
150-
if (!models) {
159+
if (!stt.models) {
151160
getModels();
152161
}
153162
}
154163
}
155164

156165
// Populate the quality select field
157166
function populateBands() {
158-
if (!bands && models) {
159-
bands = models.map(function(m) {
167+
if (!bands && stt.models) {
168+
bands = stt.models.map(function(m) {
160169
return m.name.split('_')[1];
161170
});
162171
var unique_bands = bands.filter(onlyUnique);
@@ -211,17 +220,15 @@
211220
// Retrieve the available models from the server, if data is returned, then
212221
// can enable the dynamic selection fields.
213222
function getModels(haveCredentials) {
214-
console.log('Fetching models');
215223
var u = $('#node-input-username').val();
216224
var p = $('#node-input-password').val();
217225

218-
$.getJSON('/watson-speech-to-text/models/', {un: u, pwd: p}).done(function (data) {
219-
console.log('Models have been retrieved');
226+
$.getJSON('watson-speech-to-text/models/', {un: u, pwd: p}).done(function (data) {
220227
if (data.error) {
221228
$('label#node-label-message').parent().show();
222229
$('label#node-label-message').text(data.error);
223230
} else if (data.models) {
224-
models = data.models;
231+
stt.models = data.models;
225232
//have_credentials = true;
226233
postModelCheck();
227234
}
@@ -234,8 +241,8 @@
234241

235242
// Called to complete the languages selection table
236243
function processLanguages() {
237-
if (!languages && models) {
238-
languages = models.map(function(m) {
244+
if (!languages && stt.models) {
245+
languages = stt.models.map(function(m) {
239246
return m.language;
240247
});
241248
}
@@ -262,7 +269,7 @@
262269

263270
// Called to work through the models, completing the dyanmic selection fields.
264271
function processModels() {
265-
if (models) {
272+
if (stt.models) {
266273
processLanguages();
267274
populateBands();
268275
}
@@ -284,7 +291,6 @@
284291
// This is the on edit prepare function, which will be invoked everytime the dialog
285292
// is shown.
286293
function oneditprepare() {
287-
console.log('In oneditprepare');
288294
hideEverything();
289295
restoreFromHidden();
290296
handlersUI();
@@ -295,7 +301,7 @@
295301
.done(function (service) {
296302
restoreFromHidden();
297303
$('.credentials').toggle(!service);
298-
if (!models) {getModels(service);}
304+
if (!stt.models) {getModels(service);}
299305
else {postModelCheck();}
300306
})
301307
.fail(function () {
@@ -342,4 +348,5 @@
342348
oneditsave: oneditsave
343349
});
344350
})();
351+
345352
</script>

services/speech_to_text/v1.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ module.exports = function (RED) {
5959

6060
// API used by widget to fetch available models
6161
RED.httpAdmin.get('/watson-speech-to-text/models', function (req, res) {
62-
console.log('Insde request to fetch models');
63-
console.log('Checking bound credentials username / password', username, password);
6462
var stt = watson.speech_to_text({
6563
username: username ? username : req.query.un,
6664
password: password ? password : req.query.pwd,
@@ -70,10 +68,8 @@ module.exports = function (RED) {
7068

7169
stt.getModels({}, function(err, models){
7270
if (err) {
73-
console.log('Error fetching models ', err);
7471
res.json(err);
7572
} else {
76-
console.log('Models returned');
7773
res.json(models);
7874
}
7975
});

0 commit comments

Comments
 (0)