Created
April 6, 2012 16:38
-
-
Save aroman/2321203 to your computer and use it in GitHub Desktop.
*Ehm*, this shit's got to go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EditableMixin = { | |
useCriterion: function () { | |
if (this.criterion) { | |
return this.criterion; | |
} else { | |
return ""; | |
} | |
}, | |
saveKey: function (event) { | |
if (event.keyCode === 13 || event.type === 'focusout') { | |
var input = this.$(event.target); | |
var display = this.$(":[data-target='" + input.data('role') + "']" + this.useCriterion()); | |
var role = input.data('role'); | |
var value = input.val(); | |
// If the value was actually changed. | |
if (value !== this.model.get(role)) { | |
if (role === "course_title") { | |
var titles = courses.pluck('title'); | |
if (_.indexOf(titles, value) === -1) { | |
input.hide(); | |
display.show(); | |
input.val(''); | |
return; | |
} | |
var course = courses.find(function (course) { | |
return course.get("title") === value; | |
}); | |
this.model.set("course_id", course.get('id')); | |
} | |
this.model.set(role, value); | |
display.html(this.model.get(role)); | |
this.model.save(); | |
courses.fetch(); | |
} | |
input.hide(); | |
display.show(); | |
} | |
}, | |
editKey: function (event) { | |
var display = this.$(event.target); | |
var input = this.$(":[data-role='" + display.data('target') + "']" + this.useCriterion()); | |
display.hide(); | |
input.show(); | |
input.focus(); | |
} | |
}; | |
var mergeMixin = function (view, mixin) { | |
_.defaults(view.prototype, mixin); | |
_.defaults(view.prototype.events, mixin.events); | |
if (mixin.initialize !== undefined) { | |
var oldInitialize = view.prototype.initialize; | |
view.prototype.initialize = function () { mixin.initialize.apply(this); oldInitialize.apply(this); }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment