~rharding/juju-gui/renderBundle2

« back to all changes in this revision

Viewing changes to app/store/charm.js

  • Committer: Jeff Pihach
  • Date: 2013-08-16 20:16:31 UTC
  • mto: This revision was merged to the branch mainline in revision 963.
  • Revision ID: jeff.pihach@canonical.com-20130816201631-201a4u7qfec333c9
upgraded jshint to current version

Show diffs side-by-side

added added

removed removed

Lines of Context:
126
126
     * Api call to fetch a charm's details, with an optional local cache.
127
127
     *
128
128
     * @method charmWithCache
129
 
     * @param {String} charmID the charm to fetch This is the fully qualified
130
 
     * charm name in the format scheme:series/charm-revision..
 
129
     * @param {String} charmID the charm to fetch.
131
130
     * @param {Object} callbacks the success/failure callbacks to use.
132
131
     * @param {Object} bindScope the scope of *this* in the callbacks.
133
132
     * @param {ModelList} cache a local cache of browser charms.
134
133
     */
135
 
    charm: function(charmID, callbacks, bindScope, cache, defaultSeries) {
 
134
    charm: function(charmID, callbacks, bindScope, cache) {
136
135
      if (bindScope) {
137
136
        callbacks.success = Y.bind(callbacks.success, bindScope);
138
137
      }
156
155
          };
157
156
        }
158
157
      }
159
 
      this._charm(this.normalizeCharmId(charmID, defaultSeries),
160
 
                  callbacks, bindScope);
 
158
      this._charm(charmID, callbacks, bindScope);
161
159
    },
162
160
 
163
161
    /**
166
164
     @method promiseCharm
167
165
     @param {String} charmId to fetch.
168
166
     @param {ModelList} cache a local cache of browser charms.
169
 
     @param {String} defaultSeries to resolve.
170
167
     @return {Promise} to load charm. Triggered with same result
171
168
             as this.charm.
172
169
    */
173
 
    promiseCharm: function(charmId, cache, defaultSeries) {
 
170
    promiseCharm: function(charmId, cache) {
174
171
      var self = this;
175
172
      return Y.Promise(function(resolve, reject) {
176
173
        self.charm(charmId, { 'success': resolve, 'failure': reject },
177
 
            self, cache, defaultSeries);
 
174
            self, cache);
178
175
      });
179
176
    },
180
177
 
181
178
    /**
182
 
     Normalize a charm name so we can request its full data. Charm lookup
183
 
     requires a very specific form of the charm identifier.
184
 
 
185
 
     series/charm-revision
186
 
 
187
 
     where revision can currently be any numeric placeholder.
188
 
 
189
 
     @method normalizeCharmId
190
 
     @param {String} charmId to normalize.
191
 
     @param {String} defaultSeries to resolve.
192
 
     @return {String} normalized id.
193
 
     */
194
 
    normalizeCharmId: function(charmId, defaultSeries) {
195
 
      var result = charmId;
196
 
      if (/^(cs:|local:)/.exec(result)) {
197
 
        result = result.slice(result.indexOf(':') + 1);
198
 
      }
199
 
 
200
 
      if (result.indexOf('/') === -1) {
201
 
        if (!defaultSeries) {
202
 
          console.warn('Unable to normalize charm id, no defaultSeries');
203
 
          defaultSeries = 'precise';
204
 
        }
205
 
        result = defaultSeries + '/' + result;
206
 
      }
207
 
      if (/\-\d+/.exec(result) === null) {
208
 
        // Add in a revision placeholder
209
 
        result = result + '-1';
210
 
      }
211
 
      return result;
212
 
    },
213
 
 
214
 
    /**
215
 
      Promises to return the latest charm ID for a given charm if a newer one
216
 
      exists; this also caches the newer charm if one is available.
217
 
 
218
 
      @method promiseUpgradeAvailability
219
 
      @param {Charm} charm an existing charm potentially in need of an upgrade.
220
 
      @param {ModelList} cache a local cache of browser charms.
221
 
      @return {Promise} with an id or undefined.
222
 
    */
223
 
    promiseUpgradeAvailability: function(charm, cache) {
224
 
      // Get the charm's store ID, then replace the version number
225
 
      // with '-HEAD' to retrieve the latest version of the charm.
226
 
      var storeId = charm.get('storeId').replace(/-\d+$/, '-HEAD');
227
 
      return this.promiseCharm(storeId, cache)
228
 
        .then(function(latest) {
229
 
            var latestVersion = parseInt(latest.charm.id.split('-').pop(), 10),
230
 
               currentVersion = parseInt(charm.get('revision'), 10);
231
 
            if (latestVersion > currentVersion) {
232
 
              return latest.charm.id;
233
 
            }
234
 
          }, function(e) {
235
 
            throw e;
236
 
          });
237
 
    },
238
 
 
239
 
    /**
240
179
     * Api call to search charms
241
180
     *
242
181
     * @method search