-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathtie.js
More file actions
97 lines (86 loc) · 2.07 KB
/
tie.js
File metadata and controls
97 lines (86 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
steal('jquery/controller').then(function($){
/**
* @class jQuery.Tie
* @core
*
* The $.fn.tie plugin binds form elements and controllers with
* models and vice versa. The result is that a change in
* a model will automatically update the form element or controller
* AND a change event on the element will update the model.
*
*
*
*
*
*/
$.Controller("jQuery.Tie",{
setup : function(el){
this._super(el,{})
return $.makeArray(arguments);
},
init : function(el, inst, attr, type){
// if there's a controller
if(!type){
//find the first one that implements val
var controllers = this.element.data("controllers") || {};
for(var name in controllers){
var controller = controllers[name];
if(typeof controller.val == 'function'){
type = name;
break;
}
}
}
this.type = type;
this.attr = attr;
this.inst = inst;
this.bind(inst, attr, "attrChanged");
//destroy this controller if the model instance is destroyed
this.bind(inst, "destroyed", "modelDestroyed");
var value = inst.attr(attr);
//set the value
this.lastValue = value;
if(type){
//destroy this controller if the controller is destroyed
this.bind(this.element.data("controllers")[type],"destroyed","destroy");
this.element[type]("val",value);
}else{
this.element.val(value)
}
},
attrChanged : function(inst, ev, val){
if (val !== this.lastValue) {
this.setVal(val);
this.lastValue = val;
}
},
modelDestroyed : function(){
this.destroy()
},
setVal : function(val){
if (this.type) {
this.element[this.type]("val", val)
}
else {
this.element.val(val)
}
},
change : function(el, ev, val){
if(!this.type && val === undefined){
val = this.element.val();
}
this.inst.attr(this.attr, val, null, this.proxy('setBack'))
},
setBack : function(){
this.setVal(this.lastValue);
},
destroy : function(){
this.inst = null;
if(! this._destroyed ){
// assume it's because of the https://github.com/jupiterjs/jquerymx/pull/20
// problem and don't throw an error
this._super();
}
}
});
});