如何使用Breeze从MongoHQ获取数据?到目前为止,我已经尝试过这个:
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);
}
});
}
并且它运作良好但现在我想对Breeze做同样的事情,这就是我所拥有的:
<!-- Knockout template -->
<ul data-bind="foreach: results">
<li>
<span data-bind="text:FirstName"></span>
<span data-bind="text:LastName"></span>
</li>
</ul>
从查询绑定到员工:
manager.executeQuery(breeze.EntityQuery.from("Customers"))
.then(function(data){
ko.applyBindings(data);
});
任何人都可以帮我这个吗? THX。
更新:我在这里做错了什么
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);
}
在控制台中我只能看到这个:
Failed to retrieve documents from remote data source anfrageerstellen.js:222
getLocals called
答案 0 :(得分:1)
我还没有真正看到你的问题是什么,所以我只是想去刺它。查看我使用任何带有Breeze的API制作的示例片段,这可能有助于找到如何连接到您的API,在这种情况下,似乎是托管Mongo DB的服务器。如果不了解您遇到的任何问题,很难提出更具体的建议 -
以下是您可以用作路线图的datacontext示例 -
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);
}
});
不知道这是否是你正在寻找的东西,你仍然可能需要做更多工作,比如设置你的模型,如果你从服务器上得到复杂的结构你可能需要将它们映射回您的对象等...但至少这应该从服务器返回相同的数据。如果没有,并且您收到任何错误,请告诉我原因,我可以尝试提供帮助。
祝你好运。