For some reason the jQuery MiniColors color picker (http://labs.abeautifulsite.net/jquery-minicolors/) seems to not work correctly with Aurelia data binding.
calendar.html (template):
<!-- src/settings/school/calendars -->
<template>
<require from="jquery-minicolors/jquery.minicolors.css"></require>
<form>
<div class="form-group">
<label class="control-label" for="cal_name_orig"><span t="Calendar_name"></span></label>
<input type="text" class="form-control" ref="cal_name_orig" value.bind="calendar.cal_name_orig & validate" />
</div>
<div class="form-group">
<label class="control-label" for="cal_color"><span t="Color"></span></label>
<input type="text" id="cal_color" class="form-control" value.bind="calendar.cal_color">
</div>
</form>
</template>
And my calendar.js (view-model)
//src/settings/school/calendars.js
import 'jquery-minicolors';
export class SettingsSchoolCalendars {
error = null;
selectedId = null;
calendar = {};
attached() {
var self = this;
// set focus when modal is shown
$("#cal_color").minicolors({
control: "wheel",
theme: "bootstrap",
});
}
}
The control works in the form, but there are two problems with the binding:
The value of calendar.cal_color appears in the input but does NOT set the color of the color picker.
When I change the color picker color, the new value appears in the input but does NOT update the binded value calendar.cal_color.
Is jQuery MiniColors messing with the value.bind property of the input control? Is there another explanation?
I can successfully get the model to update by adding this to the MiniColors instantiation:
change: function(hex, opacity) {
self.calendar.cal_color = hex;
}
But, I can't seem to do the opposite (update the MiniColors control when the model changes). And it still doesn't load the initial color correctly.
Help!
$("#cal_color")...You should use therefattribute to obtain a reference on your VM to the element you want to reference.<input ref="cal_color"...and then in your VM code$(this.cal_color)...Also, you generally don't need to dovar self = thisin ES2015. You can use a fat arrow function to get a properly set upthis. Though sometimes jQuery type stuff will mess with it. - Ashley Grant