Skip to content

Commit

Permalink
fix(resolver): custom params resolver
Browse files Browse the repository at this point in the history
implementing new LazyOptional resolver to handle various DOM positions of t and t-params

fixes issue #9 (comment)
  • Loading branch information
zewa666 committed Oct 5, 2015
1 parent 8a7481e commit 8c6a0b5
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 105 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aurelia-i18n",
"version": "0.2.3",
"version": "0.2.4",
"description": "A plugin that provides i18n support.",
"keywords": [
"aurelia",
Expand Down
15 changes: 9 additions & 6 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
System.config({
defaultJSExtensions: true,
transpiler: "babel",
babelOptions: {
"defaultJSExtensions": true,
"transpiler": "babel",
"babelOptions": {
"optional": [
"es7.decorators",
"es7.classProperties"
]
},
paths: {
"paths": {
"aurelia-i18n/*": "dist\\system/*js",
"github:*": "jspm_packages/github/*",
"npm:*": "jspm_packages/npm/*"
},
}
});

map: {
System.config({
"map": {
"Intl.js": "github:andyearnshaw/[email protected]",
"aurelia-dependency-injection": "github:aurelia/[email protected]",
"aurelia-event-aggregator": "github:aurelia/[email protected]",
Expand Down Expand Up @@ -75,3 +77,4 @@ System.config({
}
}
});

2 changes: 1 addition & 1 deletion dist/amd/base-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ define(['exports', './i18n', 'aurelia-event-aggregator'], function (exports, _i1
this.i18n = i18n;
this.element = element;

this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function (payload) {
this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function () {
_this.i18n.updateTranslations(_this.element);
});
}
Expand Down
37 changes: 20 additions & 17 deletions dist/amd/t.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['exports', './i18n', 'aurelia-event-aggregator', 'aurelia-templating', 'aurelia-dependency-injection'], function (exports, _i18n, _aureliaEventAggregator, _aureliaTemplating, _aureliaDependencyInjection) {
define(['exports', './i18n', 'aurelia-event-aggregator', 'aurelia-templating', './utils'], function (exports, _i18n, _aureliaEventAggregator, _aureliaTemplating, _utils) {
'use strict';

Object.defineProperty(exports, '__esModule', {
Expand Down Expand Up @@ -50,7 +50,7 @@ define(['exports', './i18n', 'aurelia-event-aggregator', 'aurelia-templating', '

_createClass(TParamsCustomAttribute, [{
key: 'valueChanged',
value: function valueChanged(newValue, oldValue) {}
value: function valueChanged() {}
}]);

var _TParamsCustomAttribute = TParamsCustomAttribute;
Expand All @@ -63,7 +63,7 @@ define(['exports', './i18n', 'aurelia-event-aggregator', 'aurelia-templating', '
var TCustomAttribute = (function () {
_createClass(TCustomAttribute, null, [{
key: 'inject',
value: [Element, _i18n.I18N, _aureliaEventAggregator.EventAggregator, _aureliaDependencyInjection.Optional.of(TParamsCustomAttribute)],
value: [Element, _i18n.I18N, _aureliaEventAggregator.EventAggregator, _utils.LazyOptional.of(TParamsCustomAttribute)],
enumerable: true
}]);

Expand All @@ -72,34 +72,37 @@ define(['exports', './i18n', 'aurelia-event-aggregator', 'aurelia-templating', '

this.element = element;
this.service = i18n;
this.params = tparams;
this.ea = ea;
this.lazyParams = tparams;
}

_createClass(TCustomAttribute, [{
key: 'bind',
value: function bind() {
var _this = this;

if (this.params) {
this.params.valueChanged = function (newParams, oldParams) {
_this.paramsChanged(_this.value, newParams, oldParams);
};
}

var p = this.params !== null ? this.params.value : undefined;

this.subscription = this.ea.subscribe('i18n:locale:changed', function () {
_this.service.updateValue(_this.element, _this.value, p);
});
this.params = this.lazyParams();

setTimeout(function () {
_this.service.updateValue(_this.element, _this.value, p);
if (_this.params) {
_this.params.valueChanged = function (newParams, oldParams) {
_this.paramsChanged(_this.value, newParams, oldParams);
};
}

var p = _this.params !== null ? _this.params.value : undefined;
_this.subscription = _this.ea.subscribe('i18n:locale:changed', function () {
_this.service.updateValue(_this.element, _this.value, p);
});

setTimeout(function () {
_this.service.updateValue(_this.element, _this.value, p);
});
});
}
}, {
key: 'paramsChanged',
value: function paramsChanged(newValue, newParams, oldParams) {
value: function paramsChanged(newValue, newParams) {
this.service.updateValue(this.element, newValue, newParams);
}
}, {
Expand Down
46 changes: 45 additions & 1 deletion dist/amd/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
define(['exports'], function (exports) {
define(['exports', 'aurelia-dependency-injection'], function (exports, _aureliaDependencyInjection) {
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var extend = function extend(destination, source) {
for (var property in source) {
destination[property] = source[property];
Expand All @@ -28,5 +37,40 @@ define(['exports'], function (exports) {

return opts;
};

exports.assignObjectToKeys = assignObjectToKeys;

var LazyOptional = (function (_Resolver) {
_inherits(LazyOptional, _Resolver);

function LazyOptional(key) {
_classCallCheck(this, LazyOptional);

_get(Object.getPrototypeOf(LazyOptional.prototype), 'constructor', this).call(this);
this.key = key;
}

_createClass(LazyOptional, [{
key: 'get',
value: function get(container) {
var _this = this;

return function () {
if (container.hasHandler(_this.key, false)) {
return container.get(_this.key);
}
return null;
};
}
}], [{
key: 'of',
value: function of(key) {
return new LazyOptional(key);
}
}]);

return LazyOptional;
})(_aureliaDependencyInjection.Resolver);

exports.LazyOptional = LazyOptional;
});
2 changes: 1 addition & 1 deletion dist/commonjs/base-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var BaseI18N = (function () {
this.i18n = i18n;
this.element = element;

this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function (payload) {
this.__i18nDisposer = ea.subscribe('i18n:locale:changed', function () {
_this.i18n.updateTranslations(_this.element);
});
}
Expand Down
37 changes: 20 additions & 17 deletions dist/commonjs/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _aureliaEventAggregator = require('aurelia-event-aggregator');

var _aureliaTemplating = require('aurelia-templating');

var _aureliaDependencyInjection = require('aurelia-dependency-injection');
var _utils = require('./utils');

var TValueConverter = (function () {
_createClass(TValueConverter, null, [{
Expand Down Expand Up @@ -57,7 +57,7 @@ var TParamsCustomAttribute = (function () {

_createClass(TParamsCustomAttribute, [{
key: 'valueChanged',
value: function valueChanged(newValue, oldValue) {}
value: function valueChanged() {}
}]);

var _TParamsCustomAttribute = TParamsCustomAttribute;
Expand All @@ -70,7 +70,7 @@ exports.TParamsCustomAttribute = TParamsCustomAttribute;
var TCustomAttribute = (function () {
_createClass(TCustomAttribute, null, [{
key: 'inject',
value: [Element, _i18n.I18N, _aureliaEventAggregator.EventAggregator, _aureliaDependencyInjection.Optional.of(TParamsCustomAttribute)],
value: [Element, _i18n.I18N, _aureliaEventAggregator.EventAggregator, _utils.LazyOptional.of(TParamsCustomAttribute)],
enumerable: true
}]);

Expand All @@ -79,34 +79,37 @@ var TCustomAttribute = (function () {

this.element = element;
this.service = i18n;
this.params = tparams;
this.ea = ea;
this.lazyParams = tparams;
}

_createClass(TCustomAttribute, [{
key: 'bind',
value: function bind() {
var _this = this;

if (this.params) {
this.params.valueChanged = function (newParams, oldParams) {
_this.paramsChanged(_this.value, newParams, oldParams);
};
}

var p = this.params !== null ? this.params.value : undefined;

this.subscription = this.ea.subscribe('i18n:locale:changed', function () {
_this.service.updateValue(_this.element, _this.value, p);
});
this.params = this.lazyParams();

setTimeout(function () {
_this.service.updateValue(_this.element, _this.value, p);
if (_this.params) {
_this.params.valueChanged = function (newParams, oldParams) {
_this.paramsChanged(_this.value, newParams, oldParams);
};
}

var p = _this.params !== null ? _this.params.value : undefined;
_this.subscription = _this.ea.subscribe('i18n:locale:changed', function () {
_this.service.updateValue(_this.element, _this.value, p);
});

setTimeout(function () {
_this.service.updateValue(_this.element, _this.value, p);
});
});
}
}, {
key: 'paramsChanged',
value: function paramsChanged(newValue, newParams, oldParams) {
value: function paramsChanged(newValue, newParams) {
this.service.updateValue(this.element, newValue, newParams);
}
}, {
Expand Down
48 changes: 47 additions & 1 deletion dist/commonjs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
Object.defineProperty(exports, '__esModule', {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _aureliaDependencyInjection = require('aurelia-dependency-injection');

var extend = function extend(destination, source) {
for (var property in source) {
destination[property] = source[property];
Expand All @@ -27,4 +38,39 @@ var assignObjectToKeys = function assignObjectToKeys(root, obj) {

return opts;
};
exports.assignObjectToKeys = assignObjectToKeys;

exports.assignObjectToKeys = assignObjectToKeys;

var LazyOptional = (function (_Resolver) {
_inherits(LazyOptional, _Resolver);

function LazyOptional(key) {
_classCallCheck(this, LazyOptional);

_get(Object.getPrototypeOf(LazyOptional.prototype), 'constructor', this).call(this);
this.key = key;
}

_createClass(LazyOptional, [{
key: 'get',
value: function get(container) {
var _this = this;

return function () {
if (container.hasHandler(_this.key, false)) {
return container.get(_this.key);
}
return null;
};
}
}], [{
key: 'of',
value: function of(key) {
return new LazyOptional(key);
}
}]);

return LazyOptional;
})(_aureliaDependencyInjection.Resolver);

exports.LazyOptional = LazyOptional;
2 changes: 1 addition & 1 deletion dist/es6/base-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class BaseI18N {
this.i18n = i18n;
this.element = element;

this.__i18nDisposer = ea.subscribe('i18n:locale:changed', payload => {
this.__i18nDisposer = ea.subscribe('i18n:locale:changed', () => {
this.i18n.updateTranslations(this.element);
});
}
Expand Down
Loading

0 comments on commit 8c6a0b5

Please sign in to comment.