我想获得像github这样的api的JSON数据。这是正确的方法吗?
使用Javascript:
var Github = window.Github = Ember.Application.create({
LOG_TRANSITIONS: true
});
Github.Adapter = DS.Adapter.extend({
find: function(store, type, id) {
//var url = type.url;
var url = "https://api.github.com/repos/emberjs/ember.js/commits";
//url = url.fmt(id);
jQuery.getJSON(url, function(data) {
// data is a hash of key/value pairs. If your server returns a
// root, simply do something like:
// store.push(type, id, data.person)
console.dir(data);
store.push(type, id, data);
});
}
});
Github.Commit = DS.Model.extend({
sha: DS.attr('string'),
url: DS.attr('string')
});
Github.Store = DS.Store.extend({
adapter: 'Github.Adapter'
});
Github.IndexRoute = Ember.Route.extend({
model: function() {
return Github.Store.find('commit');
}
});
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="http://builds.emberjs.com/tags/v1.2.0/ember.js"></script>
<script src="http://builds.emberjs.com/tags/v1.0.0-beta.3/ember-data.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
但问题是什么?我不明白如何解释断言。
答案 0 :(得分:0)
找到一个解决方案,它正在运行,但我不确定这是否正确:)
的JavaScript
var Github = window.Github = Ember.Application.create({
LOG_TRANSITIONS: true
});
Github.ApplicationSerializer = DS.RESTSerializer.extend({
extractArray: function(store, type, payload, id, requestType) {
payload.forEach(function(element, key){
element.id = element[type.idField];
});
var newPayload = {};
newPayload[Ember.String.pluralize(type.typeKey)] = payload;
return this._super(store, type, newPayload, id, requestType);
}
});
Github.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'repos/emberjs/ember.js',
host: 'https://api.github.com'
});
Github.Commit = DS.Model.extend({
sha: DS.attr('string'),
url: DS.attr('string')
});
Github.Commit.idField = 'sha';
Github.IndexRoute = Ember.Route.extend({
setupController: function(){
var store = this.get('store');
store.find('commit');
}
});