Skip to content

Commit

Permalink
Merge pull request tastejs#707 from kamilogorek/backbone-trimming
Browse files Browse the repository at this point in the history
Backbone input value trimming on edit
  • Loading branch information
sindresorhus committed Oct 31, 2013
2 parents 309e4aa + a7c8514 commit 503a16d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 9 additions & 2 deletions architecture-examples/backbone/js/views/todo-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ var app = app || {};

// Close the `"editing"` mode, saving changes to the todo.
close: function () {
var trimmedValue = this.$input.val().trim();
this.$input.val(trimmedValue);
var value = this.$input.val();
var trimmedValue = value.trim();

if (trimmedValue) {
this.model.save({ title: trimmedValue });

if (value !== trimmedValue) {
// Model values changes consisting of whitespaces only are not causing change to be triggered
// Therefore we've to compare untrimmed version with a trimmed one to chech whether anything changed
// And if yes, we've to trigger change event ourselves
this.model.trigger('change');
}
} else {
this.clear();
}
Expand Down
16 changes: 12 additions & 4 deletions dependency-examples/backbone_require/js/views/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ define([

// Close the `"editing"` mode, saving changes to the todo.
close: function () {
var value = this.$input.val().trim();

if (value) {
this.model.save({ title: value });
var value = this.$input.val();
var trimmedValue = value.trim();

if (trimmedValue) {
this.model.save({ title: trimmedValue });

if (value !== trimmedValue) {
// Model values changes consisting of whitespaces only are not causing change to be triggered
// Therefore we've to compare untrimmed version with a trimmed one to chech whether anything changed
// And if yes, we've to trigger change event ourselves
this.model.trigger('change');
}
} else {
this.clear();
}
Expand Down

0 comments on commit 503a16d

Please sign in to comment.