Skip to content

Commit

Permalink
Merge pull request #10525 from raytiley/patch-1
Browse files Browse the repository at this point in the history
Don't render attr if current value is same as new value
  • Loading branch information
rwjblue committed Feb 25, 2015
2 parents bb4ddec + 72fb390 commit 922ab71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/ember-htmlbars/tests/helpers/input_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ QUnit.test("input tabindex is updated when setting tabindex property of view", f
equal(view.$('input').attr('tabindex'), "3", "updates text field after tabindex changes");
});

QUnit.test("cursor position is not lost when updating content", function() {
equal(view.$('input').val(), "hello", "precondition - renders text field with value");

var $input = view.$('input');
var input = $input[0];

// set the cursor position to 3 (no selection)
run(function() {
input.value = 'derp';
input.selectionStart = 3;
input.selectionEnd = 3;
});

run(null, set, controller, 'val', 'derp');

equal(view.$('input').val(), "derp", "updates text field after value changes");

equal(input.selectionStart, 3, 'cursor position was not lost');
equal(input.selectionEnd, 3, 'cursor position was not lost');
});


QUnit.module("{{input type='text'}} - static values", {
setup: function() {
controller = {};
Expand Down
5 changes: 5 additions & 0 deletions packages/ember-views/lib/attr_nodes/attr_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ AttrNode.prototype.render = function render(buffer) {
value = '';
}

// If user is typing in a value we don't want to rerender and loose cursor position.
if (this.attrName === 'value' && this._morph.element.value === value) {
return;
}

if (this.lastValue !== null || value !== null) {
this._morph.setContent(value);
this.lastValue = value;
Expand Down

0 comments on commit 922ab71

Please sign in to comment.