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

Unified Diff: app/models/model-controller.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/models/charm.js ('k') | app/models/models.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: app/models/model-controller.js
=== added file 'app/models/model-controller.js'
--- app/models/model-controller.js 1970-01-01 00:00:00 +0000
+++ app/models/model-controller.js 2013-04-30 15:49:27 +0000
@@ -0,0 +1,128 @@
+'use strict';
+
+YUI.add('model-controller', function(Y) {
+ /**
+ Provides a collection of utility methods to interact
+ with the db and it's models.
+
+ @class ModelController
+ @constructor
+ */
+ var ModelController = Y.Base.create('juju-model-controller', Y.Base, [], {
+
+ /**
+ Returns a promise for a fully populated charm model.
+
+ @method getCharm
+ @param {String} charmId The charmId that you want to populate.
+ @return {Y.Promise} A promise for the charm model.
+ */
+ getCharm: function(charmId) {
+ var db = this.get('db'),
+ env = this.get('env');
+
+ return new Y.Promise(
+ function(resolve, reject) {
+ var charm = db.charms.getById(charmId);
+ if (charm && charm.loaded) {
+ resolve(charm);
+ } else {
+ charm = db.charms.add({id: charmId}).load(env,
+ // If views are bound to the charm model, firing "update" is
+ // unnecessary, and potentially even mildly harmful.
+ function(err, data) {
+ db.fire('update');
+ resolve(db.charms.getById(charmId));
+ });
+ }
+ });
+ },
+
+ /**
+ Returns a promise for a fully populated service model or an error.
+
+ @method getService
+ @param {String} serviceId A string ID for the service to fetch.
+ @return {Y.Promise} A promise for fully populated service data.
+ */
+ getService: function(serviceId) {
+ var db = this.get('db'),
+ env = this.get('env');
+
+ return new Y.Promise(
+ function(resolve, reject) {
+ // `this` points to the serviceList
+ var service = db.services.getById(serviceId);
+ // If the service and all data has already been loaded, resolve.
+ if (service && service.get('loaded')) {
+ resolve(service);
+ return;
+ }
+
+ if (!service || !service.get('loaded')) {
+ env.get_service(serviceId, function(result) {
+ if (result.err) {
+ // The service doesn't exist
+ reject(result);
+ } else {
+ var service = db.services.getById(result.service_name);
+ service.setAttrs({
+ 'config': result.result.config,
+ 'constraints': result.result.constraints,
+ 'loaded': true
+ });
+ resolve(service);
+ }
+ });
+ }
+ });
+ },
+
+ /**
+ Populates the service and charm data for the supplied service id and
+ returns a promise that you can use to know when it's ready to go.
+
+ @method populateService
+ @param {String} serviceId The service id to populate.
+ */
+ populateService: function(serviceId) {
+ var db = this.get('db'),
+ env = this.get('env'),
+ mController = this;
+
+ return new Y.Promise(
+ // this is being bound to pass additional information into the fn
+ function(resolve, reject) {
+ mController.getService(serviceId).then(function(service) {
+ mController.getCharm(service.get('charm')).then(function(charm) {
+ resolve({service: service, charm: charm});
+ }, reject);
+ }, reject);
+ });
+ }
+
+ }, {
+ ATTRS: {
+ /**
+ Reference to the client env.
+
+ @attribute env
+ @type {Y.Base}
+ @default undefined
+ */
+ env: {},
+
+ /**
+ Reference to the client db.
+
+ @attribute db
+ @type {Y.Base}
+ @default undefined
+ */
+ db: {}
+ }
+ });
+
+ Y.namespace('juju').ModelController = ModelController;
+
+}, '', { requires: ['base-build', 'base', 'promise'] });
« no previous file with comments | « app/models/charm.js ('k') | app/models/models.js » ('j') | no next file with comments »

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