[英]How can I get Tweet Timeline with using Backbone.js?
I'm a beginner for Backbone.js. 我是Backbone.js的初学者。 I've finished some tutorials.
我已经完成了一些教程。
I want to create a non-tutorial app by myself and try to create simple app that display tweet timeline using Backbone Collection or Model. 我想自己创建一个非教程应用程序,并尝试使用Backbone Collection或Model创建一个显示推文时间轴的简单应用程序。
Here is my code. 这是我的代码。 (oauth.js and sha1.js are included in HTML)
(oauth.js和sha1.js包含在HTML中)
$(function(){
var Tweet = Backbone.Model.extend({
});
var Twitter = Backbone.Collection.extend({
model: Tweet,
initialize: function(api){
this.consumerKey = //consumerKey;
this.consumerSecret = //consumerSecret;
this.accessToken = //accessToken;
this.accessTokenSecret = //accessTokenSecret;
this.message = {
method: "GET",
action: api,
parameters: {
oauth_version: "1.0",
oauth_signature_method: "HMAC-SHA1",
oauth_consumer_key: this.consumerKey,
oauth_token: this.accessToken
}
};
},
getTimeline: function(){
var accessor = {
consumerSecret: this.consumerSecret,
tokenSecret: this.accessTokenSecret
};
OAuth.setTimestampAndNonce(this.message);
OAuth.SignatureMethod.sign(this.message, accessor);
this.url = OAuth.addToURL(this.message.action, this.message.parameters);
var options = {
success: function(data, res){
console.log(data);
console.log(res);
}
};
this.fetch(options);
},
sync: function(method, model, options){
options.timeout = 10000;
options.dataType = 'jsonp';
return Backbone.sync(method, model, options);
}
});
var twitter = new Twitter("https://api.twitter.com/1.1/statuses/home_timeline.json");
twitter.getTimeline();
});
When I refreshed HTML page but 401 authorized massage is displayed in the console of Chrome Developer Tool. 当我刷新HTML页面时,Chrome开发者工具的控制台中显示401授权的消息。 I could get timeline without Backbone.js.
没有Backbone.js,我可以得到时间表。
Please teach me how should I fix it. 请教我如何解决它。
Thank you for your kindness. 谢谢你的好意。
If you could get it work without Backbone.js, you could use the fetch method without Backbone. 如果可以在没有Backbone.js的情况下使它工作,则可以使用没有Backbone的fetch方法。 Anyways the sync method is only help full if you do REST, like getting, updating, creating and deleting methods.
无论如何,仅当您执行REST(例如获取,更新,创建和删除方法)时,sync方法才完全有用。
In your case there is just a fetch right? 在您的情况下,只有获取权限吗? So override your fetch function and do whatever you done before pluging backbone in. listen to your own success and then do the Collection.reset.
因此,请覆盖提取功能,并在插入骨干网之前执行所有操作。听您自己的成功,然后执行Collection.reset。
var collection = Backbone.Collection.extend({
_onLoaded : function(data) {
this.reset(data, {parse:true});
},
fetch : function() {
$.get("").success(_.bind(this._onLoaded, this));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.