3

How can I fetch data from MongoHQ with Breeze? So far I have tried this:

this.getDataFormServer = function (formElement) {

    $.ajax({    
        url: "https://api.mongohq.com/databases/mydataBase/collections/customers/documents?_apikey=aabbddkkddiieeoollddd33kk3",
        type: "GET",
        data: {},
        datatype: "json",
        processData: false,
        contentType: "application/json; charset=utf-8",
        success: function (resultSuccess) {
            //alert("Success: " + JSON.stringify(resultSuccess));    
            console.log(resultSuccess);
        },
        complete: function (response) {
            //alert('your datas are now saved');
        },
        error: function (xhr, status, error) {
            console.log(status);
        }
    });
}

and it's working well but now I want to do the same with Breeze and here is what I have:

<!-- Knockout template -->
<ul data-bind="foreach: results">
  <li>
    <span data-bind="text:FirstName"></span>
    <span data-bind="text:LastName"></span>
  </li>
</ul>

bound to employees from query:

manager.executeQuery(breeze.EntityQuery.from("Customers"))
   .then(function(data){ 
       ko.applyBindings(data);
    });

Can anyone help me on this? thx.

UPDATE: what am i doing wrong here

 this.getDataFormServer = function (formElement) {
            var EntityQuery = breeze.EntityQuery;
            var manager = new breeze.EntityManager('https://api.mongohq.com/databases/myMongoHq/collections/customers/documents?_apikey=aelctgd3p3czwh6zx5uy&limit=4');

            var getRemoteDocuments = function () {
                var query = EntityQuery.from('documents');
                return manager.executeQuery(query)
                    .then(querySucceeded)
                    .fail(queryFailed);
            };
            function querySucceeded(data) { console.log('Retrieved documents from remote data source'); }
            function queryFailed(data) { console.log('Failed to retrieve documents from remote data source'); }

            var getLocaldocuments = function () {
                console.log("getLocals called");

                var newQuery = new EntityQuery('documents');
                var Documents = manager.executeQueryLocally(newQuery);
                if (Documents) console.log("retrieved some cars from local cache");
                else console.log("no cars retrieved from local cache");
            };

            getRemoteDocuments().then(getLocaldocuments);

        }

in the console i can only see this :

Failed to retrieve documents from remote data source anfrageerstellen.js:222
getLocals called 
Gildas.Tambo
  • 22,173
  • 7
  • 50
  • 78
  • well the get seemed to fail, but what was the http response? – Matthew James Davis Oct 03 '13 at 14:23
  • i think the problem is in the curl – Gildas.Tambo Oct 03 '13 at 15:54
  • 2
    Take a look at the Zza sample on the Breeze website. http://www.breezejs.com/samples/zza – Jay Traband Oct 03 '13 at 18:33
  • Can you give a better idea of what the problem is or what error you are getting? – PW Kad Oct 06 '13 at 16:33
  • I agree with PK Kad, a little more explanation of your issue would be really helpful. I'm not quite sure what your question is. – Jay Traband Oct 10 '13 at 19:09
  • ok if someone can just tell me how to configure the Breeze curl it will be ok like i did with ajax – Gildas.Tambo Oct 10 '13 at 19:25
  • You aren't setting up the entityManager like I showed you. In your example you are constructing the query string for Breeze, which you shouldn't be doing. It is important that you structure it the same basic way that I did, because if you look at your end point it is not what you are showing. – PW Kad Oct 14 '13 at 23:13
  • i did what you told my but it says model not define and i have no idea how it should look like maybe you can help me on it thx – Gildas.Tambo Oct 15 '13 at 06:10

1 Answers1

1

I haven't really seen what your problem is so I am just going to take a stab at it. Check out this snippet of a sample I made of using any API with Breeze that may be a bit of help in finding how to connect to your API, which in this case appears to be a server hosting a Mongo DB. Without knowing any of the problems you are having it is hard to give more specific advice -

Here is a sample of a datacontext that you could use as a road map -

function () {
    var EntityQuery = breeze.EntityQuery;

    var serviceName = 'https://api.mongohq.com/databases/mydataBase/collections/customers/'

    var myAPIKEY = "yourkeygoeshere";

    var ds = new breeze.DataService({
        serviceName: serviceName,
        // You will need to set your models up server side in a models class of some sort
        hasServerMetadata: false
    });

    function configureBreezeManager() {
        var mgr = new breeze.EntityManager({ dataService: ds });
        return mgr;
    }

    var manager = configureBreezeManager();

    // Create your models in a model file or something 
    model.initialize(manager.metadataStore);

    var metadataStore = manager.metadataStore;

    var getDocuments = function () {
        var parameters = makeParameters();
        var query = breeze.EntityQuery
            .from("documents")
            .withParameters(parameters)
            .toType('Documents');

        return manager.executeQuery(query).then(querySucceeded).fail(queryFailed);

        function querySucceeded(data) {
            return data.results;
        }
    };

    function makeParameters(addlParameters) {
        var parameters = {
            apikey: myAPIKEY
        };
        return breeze.core.extend(parameters, addlParameters);
    }

    function queryFailed(error) {
        console.log('Error retrieving data. ' + error.message);
    }
});

Don't know if this is what you are looking for and you still probably need to do more work such as setting up your models, if you are getting complex structures back from the server you probably need to map them back to your objects, etc... but at least this should get you the same data back from the server. If not, and you are getting any errors, let me know why and I can try to help.

Good luck.

PW Kad
  • 14,953
  • 7
  • 49
  • 82