Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(681)

Unified Diff: app/store/endpoints.js

Issue 8686047: Created a model controller with promises
Patch Set: Created a model controller with promises Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « app/modules-debug.js ('k') | app/store/env/fakebackend.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: app/store/endpoints.js
=== modified file 'app/store/endpoints.js'
--- app/store/endpoints.js 2013-04-27 01:22:01 +0000
+++ app/store/endpoints.js 2013-04-30 15:49:27 +0000
@@ -15,7 +15,7 @@
* This controller manages the endpoints for services, handling the events
* for the services ModelList and maintaining the endpoints map.
*
- * EndpointsController({env: Environment, db: Database})
+ * EndpointsController({db: Database})
*
* @class EndpointsController
*/
@@ -103,20 +103,6 @@
},
/**
- * Setup once('load') handler for a charm.
- *
- * @method setupCharmOnceLoad
- * @param {Object} charm The charm to watch.
- * @param {String} svcName The name of the correpsonding service.
- * @return {undefined} Nothing.
- */
- setupCharmOnceLoad: function(charm, svcName) {
- charm.once('load', Y.bind(function(svcName, evt) {
- this.addServiceToEndpointsMap(svcName, evt.currentTarget);
- }, this, svcName));
- },
-
- /**
Generic handler for a service event. If it is not pending,
make sure we have a charm. If the charm has loaded, set or update
the service's endpoints; otherwise, make that happen once the load
@@ -130,75 +116,20 @@
// If the service is not a ghost (that is, 'pending' is false),
// process it.
if (!service.get('pending')) {
- var svcName = service.get('id'),
- db = this.get('db'),
- charm_id = service.get('charm'),
- charm = db.charms.getById(charm_id),
- env = this.get('env');
-
- if (!service.get('loaded')) {
- // Call get_service to reload the service and get the full config.
- env.get_service(
- service.get('id'), Y.bind(this.loadService, this));
- }
-
- if (!charm) {
- // XXX: temp hack to work around failures here;
- charm = db.charms.add({id: charm_id});
- if (!charm) {
- charm = db.charms.getById(charm_id);
- }
- charm.load(env,
- // If views are bound to the charm model, firing "update" is
- // unnecessary, and potentially even mildly harmful.
- function(err, result) { db.fire('update'); });
- }
- if (charm.loaded) {
- this.addServiceToEndpointsMap(svcName, charm);
- } else {
- this.setupCharmOnceLoad(charm, svcName);
- }
- }
- },
-
- /**
- Callback from handlerServiceEvent get_service() call which handles
- setting the missing service attrs after a service has been added
- to the environment.
-
- @method loadService
- @param {Object} e event object returned from env.get_service().
- */
- loadService: function(e) {
- var db = this.get('db');
-
- if (e.err) {
- db.notifications.add(
- new Y.juju.models.Notification({
- title: 'Error loading service',
- message: 'Service name: ' + e.service_name,
- level: 'error'
- })
+ var mController = this.get('modelController'),
+ servicePromise = mController.populateService(service.get('id')),
+ self = this;
+
+ servicePromise.then(
+ function(data) {
+ self.addServiceToEndpointsMap(
+ data.service.get('id'), data.charm);
+ },
+ function(err) {
+ console.warn('Unable to fetch service information', err);
+ }
);
- return;
- }
- var serviceData = e.result;
- // get the service model
- var service = db.services.getById(e.service_name);
- if (!service) {
- console.warn('Could not load service data for',
- e.service_name, e);
- return;
- }
- // We intentionally ignore serviceData.rels.
- // We rely on the delta stream for relation data instead.
- service.setAttrs({
- 'config': serviceData.config,
- 'constraints': serviceData.constraints,
- 'loaded': true
- });
-
- this.handleServiceEvent(service);
+ }
},
/**
@@ -276,7 +207,33 @@
this.flatten(charm.get('provides'));
this.endpointsMap[svcName].requires =
this.flatten(charm.get('requires'));
+ // this was added to be able to test that the endpoint
+ // was successful with the new promises
+ /**
+ Fired after an endpoint was added to the endpointsMap property
+
+ @event endpointMapAdded
+ */
+ this.fire('endpointMapAdded');
}
+ }, {
+ /**
+ Reference to the client db.
+
+ @attribute db
+ @type {Y.Base}
+ @default undefined
+ */
+ db: {},
+
+ /**
+ Reference to the modelController instance
+
+ @attribute modelController
+ @type {Y.juju.ModelController}
+ @default undefined
+ */
+ modelController: {}
});
juju.EndpointsController = EndpointsController;
« no previous file with comments | « app/modules-debug.js ('k') | app/store/env/fakebackend.js » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b