简体   繁体   English

如何使用Backbone.js获取Tweet时间轴?

[英]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.

相关问题 Backbone.js-如何从集合中获取值? - Backbone.js - How I can get values from collection? 如何使用Backbone.js实现“突出显示模式”? - How can I implement a “highlight mode” using Backbone.js? 如何使用Backbone.js从服务器获取模型集合? - How can I get a collection of models from the server using Backbone.js? 使用jsfiddle:如何使用underscore.js或ribs.js库? - Using jsfiddle: how can I use underscore.js or backbone.js libraries? 如何在Backbone.js中获取错误消息? - How do I get the error messages in Backbone.js? Backbone.js - 给定一个元素,我如何获得视图? - Backbone.js - Given an element, how do I get the view? 如何获取Backbone.js发出不同的请求? - How do I get Backbone.js to issue different requests? 如何基于Backbone.js解决方案获取以前的HTTP URL或哈希? - How can I get previous HTTP url or hash based on Backbone.js solutions? 在 Backbone.js 如何让 Model 超类默认值充当子类的默认值? - In Backbone.js how can I get Model superclass defaults to act as defaults for subclasses? Backbone.js:我正在使用Web服务调用来使用Backbone.js获取集合对象,如何在html中显示集合响应对象数据? - Backbone.js: I am using web service call to get collection object using Backbone.js, How to display the collection response object data in html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM