0

I'm curious if there are any alternatives to fetching meta data from breeze when using Angular. Since $q and Q promises don't play nice with each other I decided to only use Q.js promises and not $q. In my controller I'm checking if the metaDataStore is empty:

  if (mainDataService.manager.metadataStore.isEmpty())
            {
                $log.info('metadataStore is empty') ;

                mainDataService.getMetaData()
                  .then(function ()
                  {
                     EnableCreateButton();
                  });
            }

The basic idea if I don't have meta data is to go fetch it and then enable the buttons that will allow me to create a new record.

To accomplish this I define a Q Promise in my data service then after breeze resolves the promise I resolve my promise.

  function getMetaData()
{

    var myMetaDataPromise = Q.defer();

    Q.delay(0).then(function () {

        manager.metadataStore.fetchMetadata(serviceName).then(function () {
            myMetaDataPromise.resolve();
        });

    });
    return myMetaDataPromise.promise;



}

Any suggestions to make this cleaner would be great. One thought I had was to define the promise in the controller and have the data service just return the breeze promise. I just thought that would clutter the controller up and not separate out the concerns.

pehaada
  • 513
  • 2
  • 8
  • 20
  • 1
    In the light of "Differences between Kris Kowal's Q and $q" [here](http://docs.angularjs.org/api/ng.$q), you might like to reconsider your decision to use only Q.js promises and not $q. – Beetroot-Beetroot Aug 13 '13 at 01:40

1 Answers1

1

Take a look at the new to$q extension in breeze labs on github which have been using pretty extensively to good effect.

We haven't documented it yet but the comments explain how to "install" it and use it.

Your mainDataService.getMetaData() could then be:

return manager.metadataStore.fetchMetadata(serviceName).to$q();

and subsequently you do as you intended:

mainDataService.getMetaData()
    .then(function() { EnableCreateButton(); });

You are now in the land of $q. Angular will $apply your EnableCreateButton automatically.

Ward
  • 17,793
  • 4
  • 37
  • 53