Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Created April 20, 2011 04:46
Show Gist options
  • Select an option

  • Save ColinCampbell/930392 to your computer and use it in GitHub Desktop.

Select an option

Save ColinCampbell/930392 to your computer and use it in GitHub Desktop.
diff --git a/frameworks/core_foundation/mixins/template_helpers/text_field_support.js b/frameworks/core_foundation/mixins/template_helpers/text_field_support.js
index e122f1b..a74312d 100644
--- a/frameworks/core_foundation/mixins/template_helpers/text_field_support.js
+++ b/frameworks/core_foundation/mixins/template_helpers/text_field_support.js
@@ -8,19 +8,40 @@
*/
SC.TextFieldSupport = /** @scope SC.TextFieldSupport.prototype */{
+
+ /** @private
+ Used internally to store value because the layer may not exist
+ */
+ _value: null,
+
+ /**
+ @type String
+ @default null
+ */
value: function(key, value) {
+ var $input = this.$('input');
+
if (value !== undefined) {
- this.$('input').val(value);
+ this._value = value;
+ $input.val(value);
} else {
- value = this.$('input').val();
+ if ($input.length > 0) {
+ value = this._value = $input.val();
+ } else {
+ value = this._value;
+ }
}
return value;
}.property().idempotent(),
didCreateLayer: function() {
- SC.Event.add(this.$('input'), 'focus', this, this.focusIn);
- SC.Event.add(this.$('input'), 'blur', this, this.focusOut);
+ var $input = this.$('input');
+
+ $input.val(this._value);
+
+ SC.Event.add($input, 'focus', this, this.focusIn);
+ SC.Event.add($input, 'blur', this, this.focusOut);
},
focusIn: function(event) {
diff --git a/frameworks/core_foundation/tests/mixins/template_helpers/text_field_support.js b/frameworks/core_foundation/tests/mixins/template_helpers/text_field_support.js
index 392e114..84d7870 100644
--- a/frameworks/core_foundation/tests/mixins/template_helpers/text_field_support.js
+++ b/frameworks/core_foundation/tests/mixins/template_helpers/text_field_support.js
@@ -5,10 +5,14 @@
// License: Licensed under MIT license (see license.js)
// ==========================================================================
(function() {
- var textFieldView, pane;
+ var TestObject, textFieldView, pane;
module("Text Field Support", {
setup: function() {
+ TestObject = window.TestObject = SC.Object.create({
+ value: null
+ });
+
textFieldView = SC.TemplateView.create(SC.TextFieldSupport, {
template: SC.Handlebars.compile('<input type="text">')
});
@@ -21,6 +25,7 @@
teardown: function() {
pane.remove();
+ TestObject = window.TestObject = textFieldView = pane = null;
}
});
@@ -33,6 +38,25 @@
equals(textFieldView.$('input').val(), "afterlife", "sets value of DOM to value property");
});
+ test("value binding works properly for inputs that haven't been created", function() {
+ var view = SC.TemplateView.create(SC.TextFieldSupport, {
+ template: SC.Handlebars.compile('<input type="text">'),
+ valueBinding: 'TestObject.value'
+ });
+
+ equals(view.get('value'), null, "precond - default value is null");
+ equals(view.$('input').length, 0, "precond - view doesn't have its layer created yet, thus no input element");
+
+ SC.run(function() { TestObject.set('value', 'ohai'); });
+
+ equals(view.get('value'), 'ohai', "value property was properly updated");
+
+ SC.run(function() { pane.appendChild(view); });
+
+ equals(view.get('value'), 'ohai', "value property remains the same once the view has been appended");
+ equals(view.$('input').val(), 'ohai', "value is reflected in the input element once it is created");
+ });
+
test("listens for focus and blur events", function() {
var focusCalled = 0;
var blurCalled = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment