我当前使用Vue.JS 2.0,我想从一个自定义指令中更新一个Vue实例的模型,但是我正在寻找一种不错的方法,这是因为我试图创建一个实现JQueryUI-Datepicker代码的自定义指令以下是:
<input type="text" v-datepicker="app.date" readonly="readonly"/>
Vue.directive('datepicker', {
bind: function (el, binding) {
$(el).datepicker({
onSelect: function (date) {
//this is executed every time i choose an date from datepicker
//pop.app.date = date; //this work find but is not dynamic to parent and is very dirty
Vue.set(pop, binding.expression, date); //this should work but nop
}
});
},
update: function (el, binding) {
$(el).datepicker('setDate', binding.value);
}
});
var pop = new Vue({
el: '#popApp',
data: {
app: {
date: ''
}
}
});
有人知道如何从指令以动态方式更新pop.app.date,我知道在此示例app.date和date中返回binding.expression返回在datepicker中选择的当前日期,但我不知道如何更新模型从指令
这将达到目的:
// vnode (third argument is required).
bind: function (el, binding, vnode) {
$(el).datepicker({
onSelect: function (date) {
// Set value on the binding expression.
// Here we set the date (see last argument).
(function set(obj, str, val) {
str = str.split('.');
while (str.length > 1) {
obj = obj[str.shift()];
}
return obj[str.shift()] = val;
})(vnode.context, binding.expression, date);
}
});
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句