PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Backboned/Scripts/lib/knockback-localization.js

https://bitbucket.org/mbinot/backboned
JavaScript | 145 lines | 100 code | 21 blank | 24 comment | 19 complexity | 04cf4357f5df41e244212125276eaebf MD5 | raw file
  1. /*
  2. knockback-localization.js 0.16.6
  3. (c) 2011, 2012 Kevin Malakoff - http://kmalakoff.github.com/knockback/
  4. License: MIT (http://www.opensource.org/licenses/mit-license.php)
  5. Dependencies: Knockout.js, Backbone.js, and Underscore.js.
  6. */
  7. (function() {
  8. return (function(factory) {
  9. // AMD
  10. if (typeof define === 'function' && define.amd) {
  11. return define('knockback-localization', ['underscore', 'backbone', 'knockout', 'knockback'], factory);
  12. }
  13. // CommonJS/NodeJS or No Loader
  14. else {
  15. return factory.call(this);
  16. }
  17. })(function() {// Generated by CoffeeScript 1.3.3
  18. var Backbone, kb, ko, _;
  19. kb = !this.kb && (typeof require !== 'undefined') ? require('knockback') : this.kb;
  20. _ = kb._;
  21. Backbone = kb.Backbone;
  22. ko = kb.ko;
  23. this.Knockback = this.kb = kb;
  24. if (typeof exports !== 'undefined') {
  25. module.exports = kb;
  26. }
  27. /*
  28. knockback-extensions.js (knockback-localization)
  29. (c) 2011, 2012 Kevin Malakoff.
  30. Knockback.js is freely distributable under the MIT license.
  31. See the following for full license details:
  32. https://github.com/kmalakoff/knockback/blob/master/LICENSE
  33. Dependencies: Knockout.js, Backbone.js, and Underscore.js.
  34. Optional dependency: Backbone.ModelRef.js.
  35. */
  36. kb.locale_manager = void 0;
  37. /*
  38. knockback-localized-observable.js 0.16.6
  39. (c) 2011, 2012 Kevin Malakoff.
  40. Knockback.LocalizedObservable is freely distributable under the MIT license.
  41. See the following for full license details:
  42. https://github.com/kmalakoff/knockback/blob/master/LICENSE
  43. */
  44. kb.LocalizedObservable = (function() {
  45. LocalizedObservable.extend = Backbone.Model.extend;
  46. function LocalizedObservable(value, options, vm) {
  47. var observable,
  48. _this = this;
  49. this.value = value;
  50. this.vm = vm;
  51. options || (options = {});
  52. this.vm || (this.vm = {});
  53. this.read || throwMissing(this, 'read');
  54. kb.locale_manager || throwMissing(this, 'kb.locale_manager');
  55. this.__kb || (this.__kb = {});
  56. this.__kb._onLocaleChange = _.bind(this._onLocaleChange, this);
  57. this.__kb._onChange = options.onChange;
  58. if (this.value) {
  59. value = ko.utils.unwrapObservable(this.value);
  60. }
  61. this.vo = ko.observable(!value ? null : this.read(value, null));
  62. observable = kb.utils.wrappedObservable(this, ko.dependentObservable({
  63. read: function() {
  64. if (_this.value) {
  65. ko.utils.unwrapObservable(_this.value);
  66. }
  67. _this.vo();
  68. return _this.read(ko.utils.unwrapObservable(_this.value));
  69. },
  70. write: function(value) {
  71. _this.write || throwUnexpected(_this, 'writing to read-only');
  72. _this.write(value, ko.utils.unwrapObservable(_this.value));
  73. _this.vo(value);
  74. if (_this.__kb._onChange) {
  75. return _this.__kb._onChange(value);
  76. }
  77. },
  78. owner: this.vm
  79. }));
  80. observable.destroy = _.bind(this.destroy, this);
  81. observable.observedValue = _.bind(this.observedValue, this);
  82. observable.resetToCurrent = _.bind(this.resetToCurrent, this);
  83. kb.locale_manager.bind('change', this.__kb._onLocaleChange);
  84. if (options.hasOwnProperty('default')) {
  85. observable = kb.DefaultObservable && ko.defaultObservable(observable, options["default"]);
  86. }
  87. return observable;
  88. }
  89. LocalizedObservable.prototype.destroy = function() {
  90. kb.locale_manager.unbind('change', this.__kb._onLocaleChange);
  91. this.vm = null;
  92. return kb.utils.wrappedDestroy(this);
  93. };
  94. LocalizedObservable.prototype.resetToCurrent = function() {
  95. var current_value, observable;
  96. observable = kb.utils.wrappedObservable(this);
  97. current_value = this.value ? this.read(ko.utils.unwrapObservable(this.value)) : null;
  98. if (observable() === current_value) {
  99. return;
  100. }
  101. return observable(current_value);
  102. };
  103. LocalizedObservable.prototype.observedValue = function(value) {
  104. if (arguments.length === 0) {
  105. return this.value;
  106. }
  107. this.value = value;
  108. this._onLocaleChange();
  109. };
  110. LocalizedObservable.prototype._onLocaleChange = function() {
  111. var value;
  112. value = this.read(ko.utils.unwrapObservable(this.value));
  113. this.vo(value);
  114. if (this.__kb._onChange) {
  115. return this.__kb._onChange(value);
  116. }
  117. };
  118. return LocalizedObservable;
  119. })();
  120. kb.localizedObservable = function(value, options, view_model) {
  121. return new kb.LocalizedObservable(value, options, view_model);
  122. };
  123. ; return kb;});
  124. }).call(this);