Javascript 通过第三方api使用主干
我正试图用主干网抓取instagram的订阅源。这不需要对用户进行身份验证,而是通过以下方式获取公共提要:Javascript 通过第三方api使用主干,javascript,json,backbone.js,Javascript,Json,Backbone.js,我正试图用主干网抓取instagram的订阅源。这不需要对用户进行身份验证,而是通过以下方式获取公共提要: https://api.instagram.com/v1/users//media/recent/?client_id= 我已经将JSON响应输出到控制台,但我无法将其显示在我的页面上 在下面的代码中,我使用fetchData来获取提要,我希望最终将其转换为render输出#social上所有样式化的内容。但是,尽管将提要属性设置为JSON响应,render仍然返回空对象console.l
https://api.instagram.com/v1/users//media/recent/?client_id=
我已经将JSON响应输出到控制台,但我无法将其显示在我的页面上
在下面的代码中,我使用fetchData来获取提要,我希望最终将其转换为render输出#social
上所有样式化的内容。但是,尽管将提要属性设置为JSON响应,render
仍然返回空对象<但是,fetchData
中的code>console.log会显示正确的信息
var social = {}
social.Instagram = Backbone.Model.extend();
social.InstagramFeed = Backbone.Collection.extend({
model: social.Instagram,
url: 'https://api.instagram.com/v1/users/<user_id>/media/recent/?client_id=<client_id>',
parse: function(response) {
return response.results;
},
sync: function(method, model, options) {
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: this.url,
processData: false
}, options);
return $.ajax(params);
}
});
social.InstagramView = Backbone.View.extend({
el: '#social',
feed: {},
initialize: function() {
this.collection = new social.InstagramFeed();
this.fetchData();
this.render();
},
render: function() {
console.log(this.feed);
},
fetchData: function() {
this.collection.fetch({
success: function(collection, response) {
// console.log(response);
feed = response;
// console.log(this.feed);
},
error: function() {
console.log("failed to find instagram feed...");
}
});
}
});
social.instagramview = new social.InstagramView;
var social={}
social.Instagram=Backbone.Model.extend();
social.InstagramFeed=Backbone.Collection.extend({
型号:社交。Instagram,
网址:'https://api.instagram.com/v1/users//media/recent/?client_id=',
解析:函数(响应){
返回响应结果;
},
同步:功能(方法、模型、选项){
变量参数=389;.extend({
键入:“GET”,
数据类型:“jsonp”,
url:this.url,
processData:false
},选项);
返回$.ajax(参数);
}
});
social.InstagramView=Backbone.View.extend({
el:“#社交”,
提要:{},
初始化:函数(){
this.collection=new social.InstagramFeed();
这是fetchData();
这个。render();
},
render:function(){
console.log(this.feed);
},
fetchData:function(){
这是我的收藏({
成功:功能(收集、响应){
//控制台日志(响应);
反馈=响应;
//console.log(this.feed);
},
错误:函数(){
console.log(“找不到instagram提要…”);
}
});
}
});
social.instagramview=新的social.instagramview;
我试图只使用
fetchData
函数输出信息,但是这个.el.append(response)
导致一个通知,说明el
未定义。您的render
方法在抓取完成之前被调用。您应该绑定到集合的sync
事件,并在事件处理程序中调用render
social.InstagramView = Backbone.View.extend({
el: '#social',
feed: {},
initialize: function() {
this.collection = new social.InstagramFeed();
this.fetchData();
this.collection.on('sync', function(){
this.render();
}, this);
// this.render();
},
...
})
引用Backbone.js文档:触发同步事件:
当模型或集合已成功与服务器同步时