]> git.proxmox.com Git - extjs.git/blob - extjs/packages/core/src/app/bind/BaseBinding.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / app / bind / BaseBinding.js
1 /**
2 * This class is the base for `Binding` and `MultiBinding`.
3 * @private
4 */
5 Ext.define('Ext.app.bind.BaseBinding', {
6 extend: 'Ext.util.Schedulable',
7
8 calls: 0,
9
10 kind: 20,
11
12 defaultOptions: {},
13
14 lastValue: undefined,
15
16 /**
17 * @cfg {Boolean} [single=false]
18 * This option instructs the binding to call its `destroy` method immediately after
19 * delivering the initial value.
20 * @since 5.0.0
21 */
22
23 constructor: function (owner, callback, scope, options) {
24 var me = this;
25
26 me.options = options;
27 me.owner = owner;
28 me.scope = scope;
29 me.callback = callback;
30
31 //<debug>
32 if (!callback) {
33 Ext.raise('Callback is required');
34 }
35 //</debug>
36
37 // If given a string callback name, preserve the late binding:
38 me.lateBound = Ext.isString(callback);
39 if (options && options.deep) {
40 me.deep = true;
41 }
42
43 me.callParent();
44 },
45
46 destroy: function () {
47 var me = this,
48 owner = me.owner;
49
50 me.callParent();
51 if (owner) {
52 owner.onBindDestroy(me);
53 }
54 me.scope = me.callback = me.owner = null;
55 },
56
57 isReadOnly: function() {
58 return true;
59 },
60
61 privates: {
62 getScheduler: function () {
63 var owner = this.owner;
64 return owner && owner.getScheduler();
65 },
66
67 getSession: function () {
68 var owner = this.owner;
69 return owner.isSession ? owner : owner.getSession();
70 },
71
72 notify: function (value) {
73 var me = this,
74 options = me.options || me.defaultOptions,
75 previous = me.lastValue;
76
77 // We want to deliver if:
78 // 1) We've never been called
79 // 2) We're a deep binding, which means that our object reference may not have changed,
80 // but something under us has changed. For example a link stub or a model field binding
81 // 3) If the value has changed
82 // 4) If the value is an array. It's difficult to tell if the underlying data changed
83 if (!me.calls || me.deep || previous !== value || Ext.isArray(value)) {
84 ++me.calls;
85 me.lastValue = value;
86
87 if (me.lateBound) {
88 // Interestingly, lateBound-ness may be more efficient since it does
89 // not use the "call" method.
90 me.scope[me.callback](value, previous, me);
91 } else {
92 me.callback.call(me.scope, value, previous, me);
93 }
94
95 if (options.single) {
96 me.destroy();
97 }
98 }
99 }
100 }
101 });