OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 YUI.add('model-controller', function(Y) { |
| 4 /** |
| 5 Provides a collection of utility methods to interact |
| 6 with the db and it's models. |
| 7 |
| 8 @class ModelController |
| 9 @constructor |
| 10 */ |
| 11 var ModelController = Y.Base.create('juju-model-controller', Y.Base, [], { |
| 12 |
| 13 /** |
| 14 Returns a promise for a fully populated charm model. |
| 15 |
| 16 @method getCharm |
| 17 @param {String} charmId The charmId that you want to populate. |
| 18 @return {Y.Promise} A promise for the charm model. |
| 19 */ |
| 20 getCharm: function(charmId) { |
| 21 var db = this.get('db'), |
| 22 env = this.get('env'); |
| 23 |
| 24 return new Y.Promise( |
| 25 function(resolve, reject) { |
| 26 var charm = db.charms.getById(charmId); |
| 27 if (charm && charm.loaded) { |
| 28 resolve(charm); |
| 29 } else { |
| 30 charm = db.charms.add({id: charmId}).load(env, |
| 31 // If views are bound to the charm model, firing "update" is |
| 32 // unnecessary, and potentially even mildly harmful. |
| 33 function(err, data) { |
| 34 db.fire('update'); |
| 35 resolve(db.charms.getById(charmId)); |
| 36 }); |
| 37 } |
| 38 }); |
| 39 }, |
| 40 |
| 41 /** |
| 42 Returns a promise for a fully populated service model or an error. |
| 43 |
| 44 @method getService |
| 45 @param {String} serviceId A string ID for the service to fetch. |
| 46 @return {Y.Promise} A promise for fully populated service data. |
| 47 */ |
| 48 getService: function(serviceId) { |
| 49 var db = this.get('db'), |
| 50 env = this.get('env'); |
| 51 |
| 52 return new Y.Promise( |
| 53 function(resolve, reject) { |
| 54 // `this` points to the serviceList |
| 55 var service = db.services.getById(serviceId); |
| 56 // If the service and all data has already been loaded, resolve. |
| 57 if (service && service.get('loaded')) { |
| 58 resolve(service); |
| 59 return; |
| 60 } |
| 61 |
| 62 if (!service || !service.get('loaded')) { |
| 63 env.get_service(serviceId, function(result) { |
| 64 if (result.err) { |
| 65 // The service doesn't exist |
| 66 reject(result); |
| 67 } else { |
| 68 var service = db.services.getById(result.service_name); |
| 69 service.setAttrs({ |
| 70 'config': result.result.config, |
| 71 'constraints': result.result.constraints, |
| 72 'loaded': true |
| 73 }); |
| 74 resolve(service); |
| 75 } |
| 76 }); |
| 77 } |
| 78 }); |
| 79 }, |
| 80 |
| 81 /** |
| 82 Populates the service and charm data for the supplied service id and |
| 83 returns a promise that you can use to know when it's ready to go. |
| 84 |
| 85 @method populateService |
| 86 @param {String} serviceId The service id to populate. |
| 87 */ |
| 88 populateService: function(serviceId) { |
| 89 var db = this.get('db'), |
| 90 env = this.get('env'), |
| 91 mController = this; |
| 92 |
| 93 return new Y.Promise( |
| 94 // this is being bound to pass additional information into the fn |
| 95 function(resolve, reject) { |
| 96 mController.getService(serviceId).then(function(service) { |
| 97 mController.getCharm(service.get('charm')).then(function(charm) { |
| 98 resolve({service: service, charm: charm}); |
| 99 }, reject); |
| 100 }, reject); |
| 101 }); |
| 102 } |
| 103 |
| 104 }, { |
| 105 ATTRS: { |
| 106 /** |
| 107 Reference to the client env. |
| 108 |
| 109 @attribute env |
| 110 @type {Y.Base} |
| 111 @default undefined |
| 112 */ |
| 113 env: {}, |
| 114 |
| 115 /** |
| 116 Reference to the client db. |
| 117 |
| 118 @attribute db |
| 119 @type {Y.Base} |
| 120 @default undefined |
| 121 */ |
| 122 db: {} |
| 123 } |
| 124 }); |
| 125 |
| 126 Y.namespace('juju').ModelController = ModelController; |
| 127 |
| 128 }, '', { requires: ['base-build', 'base', 'promise'] }); |
OLD | NEW |