Skip to content

Commit 8adecde

Browse files
committed
soluciones tema3
1 parent 34f4144 commit 8adecde

File tree

6 files changed

+199
-1
lines changed

6 files changed

+199
-1
lines changed

tema3/soluciones/i18n.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var I18n = (function() {
2+
var locales = {},
3+
instancia;
4+
5+
var I18n = Class.extend({
6+
init: function(locale) {
7+
this.locale = locale;
8+
},
9+
translate: function(path) {
10+
var position = locales[this.locale],
11+
path = path.split('.'),
12+
currentPath;
13+
while (currentPath = path.shift()) {
14+
position = position[currentPath];
15+
}
16+
return position;
17+
}
18+
});
19+
20+
I18n.addTranslation = function(locale, dict) {
21+
locales[locale] = dict;
22+
};
23+
24+
I18n.withCurrentLocale = function() {
25+
if (!instancia) {
26+
instancia = new this(Config.locale);
27+
}
28+
return instancia;
29+
};
30+
31+
return I18n;
32+
}());

tema3/soluciones/i18n_skeleton.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var I18n = (function() {
2+
// ???
3+
4+
var I18n = Class.extend({
5+
init: function(locale) {
6+
// ???
7+
},
8+
translate: function(path) {
9+
var position = locales[this.locale],
10+
path = path.split('.'),
11+
currentPath;
12+
while (currentPath = path.shift()) {
13+
position = position[currentPath];
14+
}
15+
return position;
16+
}
17+
});
18+
19+
I18n.addTranslation = function(locale, dict) {
20+
// ???
21+
};
22+
23+
I18n.withCurrentLocale = function() {
24+
// ???
25+
};
26+
27+
return I18n;
28+
}());
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var Observable = {
2+
mixed: function(klass) {
3+
var klass_init = klass.prototype.init;
4+
klass.prototype.init = function() {
5+
this._subscribers = {};
6+
return klass_init.apply(this, arguments);
7+
};
8+
},
9+
subscribe: function(event, callback) {
10+
this._subscribers[event] || (this._subscribers[event] = []);
11+
this._subscribers[event].push(callback);
12+
},
13+
unsubscribe: function(event, callback) {
14+
var subs = this._subscribers[event];
15+
if (!subs) { return; }
16+
for (var i=0; i<subs.length; i++) {
17+
if (subs[i] === callback) {
18+
subs.splice(i, 1);
19+
break;
20+
}
21+
}
22+
},
23+
publish: function(event) {
24+
var args = [].slice.call(arguments, 1),
25+
subscribers = this._subscribers[event];
26+
if (subscribers) {
27+
subscribers.forEach(function(sub){ sub.apply({}, args); });
28+
}
29+
}
30+
};
31+
32+
var Input = Class.extend({
33+
init: function(value) {
34+
this.value = value;
35+
},
36+
getValue: function() {
37+
return this.value;
38+
},
39+
setValue: function(value) {
40+
if (this.validate(value)) {
41+
this.value = value;
42+
this.publish('change', value);
43+
} else {
44+
this.publish('invalid', value);
45+
}
46+
},
47+
validate: function(value) {
48+
return (/^\S+$/).test(value);
49+
}
50+
});
51+
52+
Input.mixin(Observable);
53+
54+
InputView = Class.extend({
55+
init: function(input) {
56+
this.input = input;
57+
this.input.subscribe('change', bind(this, this.update));
58+
this.input.subscribe('invalid', bind(this, this.invalid));
59+
},
60+
update: function(newValue) {
61+
console.log("CHANGED: " + newValue);
62+
},
63+
invalid: function(value) {
64+
console.log("El valor: " + value + " no es válido!");
65+
}
66+
});
67+
68+
var input = new Input();
69+
var observer = new InputView(input);
70+
71+
input.setValue("Despierta!");
72+
input.setValue(" ");
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var Observable = {
2+
mixed: function(klass) {
3+
// ???
4+
},
5+
subscribe: function(event, callback) {
6+
// ???
7+
},
8+
unsubscribe: function(event, callback) {
9+
// ???
10+
},
11+
publish: function(event) {
12+
// ???
13+
};
14+
15+
var Input = Class.extend({
16+
init: function(value) {
17+
this.value = value;
18+
},
19+
getValue: function() {
20+
return this.value;
21+
},
22+
setValue: function(value) {
23+
if (this.validate(value)) {
24+
this.value = value;
25+
this.publish('change', value);
26+
} else {
27+
this.publish('invalid', value);
28+
}
29+
},
30+
validate: function(value) {
31+
return (/^\S+$/).test(value);
32+
}
33+
});
34+
35+
Input.mixin(Observable);
36+
37+
InputView = Class.extend({
38+
init: function(input) {
39+
this.input = input;
40+
this.input.subscribe('change', bind(this, this.update));
41+
this.input.subscribe('invalid', bind(this, this.invalid));
42+
},
43+
update: function(newValue) {
44+
console.log("CHANGED: " + newValue);
45+
},
46+
invalid: function(value) {
47+
console.log("El valor: " + value + " no es válido!");
48+
}
49+
});
50+
51+
var input = new Input();
52+
var observer = new InputView(input);
53+
54+
input.setValue("Despierta!");
55+
input.setValue(" ");

tema3/soluciones/klass.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,3 @@ var Class = (function(Class) {
5454

5555
return Class;
5656
}(Class || function(){}));
57-

tema3/soluciones/namespace.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var MiLib = (function(my) {
2+
my.namespace = function(string, sandbox) {
3+
var spaces = string.split('.'),
4+
root = my,
5+
space;
6+
while (space = spaces.shift()) {
7+
root = root[space] || (root[space] = {});
8+
}
9+
return sandbox(root);
10+
};
11+
return my;
12+
}(MiLib || {}));

0 commit comments

Comments
 (0)