Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 YUI.add('juju-charm-models', function(Y) { | 3 YUI.add('juju-charm-models', function(Y) { |
4 | 4 |
5 | 5 |
6 var models = Y.namespace('juju.models'); | 6 var models = Y.namespace('juju.models'); |
7 | 7 |
8 // Charms, once instantiated and loaded with data from their respective | 8 // Charms, once instantiated and loaded with data from their respective |
9 // sources, are immutable and read-only. This reflects the reality of how we | 9 // sources, are immutable and read-only. This reflects the reality of how we |
10 // interact with them. | 10 // interact with them. |
(...skipping 24 matching lines...) Expand all Loading... | |
35 // "charm.load(charm_store, optionalCallback)". The get_charm method must | 35 // "charm.load(charm_store, optionalCallback)". The get_charm method must |
36 // either callback using the default YUI approach for this code, a boolean | 36 // either callback using the default YUI approach for this code, a boolean |
37 // indicating failure, and a result; or it must return what the env version | 37 // indicating failure, and a result; or it must return what the env version |
38 // does: an object with a "result" object containing the charm data, or an | 38 // does: an object with a "result" object containing the charm data, or an |
39 // object with an "err" attribute. | 39 // object with an "err" attribute. |
40 | 40 |
41 // In both cases, environment charms and charm store charms, a charm's | 41 // In both cases, environment charms and charm store charms, a charm's |
42 // "loaded" attribute is set to true once it has all the data from its | 42 // "loaded" attribute is set to true once it has all the data from its |
43 // environment. | 43 // environment. |
44 | 44 |
45 var charm_id_re = /^(?:(\w+):)?(?:~(\S+)\/)?(\w+)\/(\S+?)-(\d+)$/, | 45 var charmIdRe = /^(?:(\w+):)?(?:~(\S+)\/)?(\w+)\/(\S+?)-(\d+)$/, |
46 id_elements = ['scheme', 'owner', 'series', 'package_name', 'revision'], | 46 idElements = ['scheme', 'owner', 'series', 'package_name', 'revision'], |
47 Charm = Y.Base.create('charm', Y.Model, [], { | 47 Charm = Y.Base.create('charm', Y.Model, [], { |
48 initializer: function() { | 48 initializer: function() { |
49 var id = this.get('id'), | 49 var id = this.get('id'), |
50 parts = id && charm_id_re.exec(id), | 50 parts = id && charmIdRe.exec(id), |
51 self = this; | 51 self = this; |
52 if (!Y.Lang.isValue(id) || !parts) { | 52 if (!Y.Lang.isValue(id) || !parts) { |
53 throw 'Developers must initialize charms with a well-formed id.'; | 53 throw 'Developers must initialize charms with a well-formed id.'; |
54 } | 54 } |
55 this.loaded = false; | 55 this.loaded = false; |
56 this.on('load', function() { this.loaded = true; }); | 56 this.on('load', function() { this.loaded = true; }); |
benjamin.saller
2012/10/26 07:56:19
Don't you need to either use self here or pass thi
gary.poster
2012/10/26 12:11:21
No, because the listener is on "this" so the conte
| |
57 parts.shift(); | 57 parts.shift(); |
58 Y.each( | 58 Y.each( |
59 Y.Array.zip(id_elements, parts), | 59 Y.Array.zip(idElements, parts), |
60 function(pair) { self.set(pair[0], pair[1]); }); | 60 function(pair) { self.set(pair[0], pair[1]); }); |
61 // full_name | 61 // full_name |
62 var tmp = [this.get('series'), this.get('package_name')], | 62 var tmp = [this.get('series'), this.get('package_name')], |
63 owner = this.get('owner'); | 63 owner = this.get('owner'); |
64 if (owner) { | 64 if (owner) { |
65 tmp.unshift('~' + owner); | 65 tmp.unshift('~' + owner); |
66 } | 66 } |
67 this.set('full_name', tmp.join('/')); | 67 this.set('full_name', tmp.join('/')); |
68 // charm_store_path | 68 // charm_store_path |
69 this.set( | 69 this.set( |
70 'charm_store_path', | 70 'charm_store_path', |
71 [(owner ? '~' + owner : 'charms'), | 71 [(owner ? '~' + owner : 'charms'), |
72 this.get('series'), | 72 this.get('series'), |
73 (this.get('package_name') + '-' + this.get('revision')), | 73 (this.get('package_name') + '-' + this.get('revision')), |
74 'json' | 74 'json' |
75 ].join('/')); | 75 ].join('/')); |
76 }, | 76 }, |
77 sync: function(action, options, callback) { | 77 sync: function(action, options, callback) { |
78 if (action !== 'read') { | 78 if (action !== 'read') { |
79 throw ( | 79 throw ( |
80 'Only use the "read" action; "' + action + '" not supported.'); | 80 'Only use the "read" action; "' + action + '" not supported.'); |
81 } | 81 } |
82 if (Y.Lang.isValue(options.get_charm)) { | 82 if (Y.Lang.isValue(options.get_charm)) { |
83 // This is an env. | 83 // This is an env. |
84 options.get_charm( | 84 options.get_charm( |
benjamin.saller
2012/10/26 07:56:19
Why the two naming styles on these_twoMethods? get
gary.poster
2012/10/26 12:11:21
As we discussed, it's because we haven't standardi
| |
85 this.get('id'), | 85 this.get('id'), |
86 function(response) { | 86 function(response) { |
87 if (response.err) { | 87 if (response.err) { |
88 callback(true, response); | 88 callback(true, response); |
89 } else if (response.result) { | 89 } else if (response.result) { |
90 callback(false, response.result); | 90 callback(false, response.result); |
91 } else { | 91 } else { |
92 // What's going on? This does not look like either of our | 92 // What's going on? This does not look like either of our |
93 // expected signatures. Declare a loading error. | 93 // expected signatures. Declare a loading error. |
94 callback(true, response); | 94 callback(true, response); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 other.get('package_name'))) || | 146 other.get('package_name'))) || |
147 (owner ? owner.localeCompare(otherOwner) : 0) || | 147 (owner ? owner.localeCompare(otherOwner) : 0) || |
148 (this.get('revision') - other.get('revision'))); | 148 (this.get('revision') - other.get('revision'))); |
149 } | 149 } |
150 } | 150 } |
151 }, { | 151 }, { |
152 ATTRS: { | 152 ATTRS: { |
153 id: { | 153 id: { |
154 writeOnce: true, | 154 writeOnce: true, |
155 validator: function(val) { | 155 validator: function(val) { |
156 return Y.Lang.isString(val) && !!charm_id_re.exec(val); | 156 return Y.Lang.isString(val) && !!charmIdRe.exec(val); |
157 } | 157 } |
158 }, | 158 }, |
159 bzr_branch: {writeOnce: true}, | 159 bzr_branch: {writeOnce: true}, |
160 charm_store_path: {writeOnce: true}, | 160 charm_store_path: {writeOnce: true}, |
161 config: {writeOnce: true}, | 161 config: {writeOnce: true}, |
162 description: {writeOnce: true}, | 162 description: {writeOnce: true}, |
163 full_name: {writeOnce: true}, | 163 full_name: {writeOnce: true}, |
164 is_subordinate: {writeOnce: true}, | 164 is_subordinate: {writeOnce: true}, |
165 last_change: { | 165 last_change: { |
166 writeOnce: true, | 166 writeOnce: true, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
214 ATTRS: {} | 214 ATTRS: {} |
215 }); | 215 }); |
216 models.CharmList = CharmList; | 216 models.CharmList = CharmList; |
217 | 217 |
218 }, '0.1.0', { | 218 }, '0.1.0', { |
219 requires: [ | 219 requires: [ |
220 'model', | 220 'model', |
221 'model-list' | 221 'model-list' |
222 ] | 222 ] |
223 }); | 223 }); |
LEFT | RIGHT |