ember_reopen.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. Merge the contents of two objects together into the first object.
  20. ```javascript
  21. Ember.merge({first: 'Tom'}, {last: 'Dale'}); // {first: 'Tom', last: 'Dale'}
  22. var a = {first: 'Yehuda'}, b = {last: 'Katz'};
  23. Ember.merge(a, b); // a == {first: 'Yehuda', last: 'Katz'}, b == {last: 'Katz'}
  24. ```
  25. @method merge
  26. @for Ember
  27. @param {Object} original The object to merge into
  28. @param {Object} updates The object to copy properties from
  29. @return {Object}
  30. */
  31. Ember.merge = function(original, updates) {
  32. for (var prop in updates) {
  33. if (!updates.hasOwnProperty(prop)) { continue; }
  34. original[prop] = updates[prop];
  35. }
  36. return original;
  37. };
  38. /**
  39. Returns true if the passed value is null or undefined. This avoids errors
  40. from JSLint complaining about use of ==, which can be technically
  41. confusing.
  42. ```javascript
  43. Ember.isNone(); // true
  44. Ember.isNone(null); // true
  45. Ember.isNone(undefined); // true
  46. Ember.isNone(''); // false
  47. Ember.isNone([]); // false
  48. Ember.isNone(function() {}); // false
  49. ```
  50. @method isNone
  51. @for Ember
  52. @param {Object} obj Value to test
  53. @return {Boolean}
  54. */
  55. Ember.isNone = function(obj) {
  56. return obj === null || obj === undefined;
  57. };
  58. /**
  59. Verifies that a value is `null` or an empty string, empty array,
  60. or empty function.
  61. Constrains the rules on `Ember.isNone` by returning false for empty
  62. string and empty arrays.
  63. ```javascript
  64. Ember.isEmpty(); // true
  65. Ember.isEmpty(null); // true
  66. Ember.isEmpty(undefined); // true
  67. Ember.isEmpty(''); // true
  68. Ember.isEmpty([]); // true
  69. Ember.isEmpty('Adam Hawkins'); // false
  70. Ember.isEmpty([0,1,2]); // false
  71. ```
  72. @method isEmpty
  73. @for Ember
  74. @param {Object} obj Value to test
  75. @return {Boolean}
  76. */
  77. Ember.isEmpty = function(obj) {
  78. return Ember.isNone(obj) || (obj.length === 0 && typeof obj !== 'function') || (typeof obj === 'object' && Ember.get(obj, 'length') === 0);
  79. };
  80. /**
  81. A value is blank if it is empty or a whitespace string.
  82. ```javascript
  83. Ember.isBlank(); // true
  84. Ember.isBlank(null); // true
  85. Ember.isBlank(undefined); // true
  86. Ember.isBlank(''); // true
  87. Ember.isBlank([]); // true
  88. Ember.isBlank('\n\t'); // true
  89. Ember.isBlank(' '); // true
  90. Ember.isBlank({}); // false
  91. Ember.isBlank('\n\t Hello'); // false
  92. Ember.isBlank('Hello world'); // false
  93. Ember.isBlank([1,2,3]); // false
  94. ```
  95. @method isBlank
  96. @for Ember
  97. @param {Object} obj Value to test
  98. @return {Boolean}
  99. */
  100. Ember.isBlank = function(obj) {
  101. return Ember.isEmpty(obj) || (typeof obj === 'string' && obj.match(/\S/) === null);
  102. };
  103. /**
  104. *
  105. */
  106. Ember.RadioButton = Ember.Checkbox.extend({
  107. tagName : "input",
  108. type : "radio",
  109. attributeBindings : [ "type", "name", "value", "checked", "style" ],
  110. style: "vertical-align: middle; margin: 0px;" ,
  111. click : function() {
  112. this.set("selection", this.$().val())
  113. },
  114. checked : function() {
  115. return this.get("value") == this.get("selection");
  116. }.property('value','selection')
  117. });
  118. /**
  119. * Set value to obj by path
  120. * Create nested objects if needed
  121. * Example:
  122. * <code>
  123. * var a = {b: {}};
  124. * var path = 'b.c.d';
  125. * var value = 1;
  126. * Em.setFullPath(a, path, value); // a = {b: {c: {d: 1}}
  127. * </code>
  128. *
  129. * @param {object} obj
  130. * @param {string} path
  131. * @param {*} value
  132. */
  133. Ember.setFullPath = function (obj, path, value) {
  134. var parts = path.split('.'),
  135. sub_path = '';
  136. parts.forEach(function(_path, _index) {
  137. Em.assert('path parts can\'t be empty', _path.length);
  138. sub_path += '.' + _path;
  139. if (_index === parts.length - 1) {
  140. Em.set(obj, sub_path, value);
  141. return;
  142. }
  143. if (Em.isNone(Em.get(obj, sub_path))) {
  144. Em.set(obj, sub_path, {});
  145. }
  146. });
  147. };
  148. Em.View.reopen({
  149. /**
  150. * overwritten set method of Ember.View to avoid uncaught errors
  151. * when trying to set property of destroyed view
  152. */
  153. set: function(attr, value){
  154. if(!this.get('isDestroyed') && !this.get('isDestroying')){
  155. this._super(attr, value);
  156. } else {
  157. console.debug('Calling set on destroyed view');
  158. }
  159. }
  160. });