如何在ajax调用后调用现有IScroll元素上的IScroll刷新方法?

时间:2018-01-19 18:36:20

标签: javascript

当在我的social-feed.js脚本中加载require.js时,我对社交媒体供稿进行了ajax调用。然后该函数调用回调来刷新在加载页面时创建的IScroll对象。 (这是我的回调)

function updateScroll(iscroll){
    var element = $('tweetwrap');

    setTimeout(function(){
        iscroll(element).refresh();
    }, 300);

}

我相信我已经按照IScroll网站上的文档进行了刷新,但它总是抛出“Uncaught TypeError:无法读取未定义的属性'样式。”

编辑:添加了创建tweetwrap元素的函数。

function getTwitter(userName, limit, callback) {

    var tweetContainer = document.createElement('div');
    tweetContainer.className = "right-rail-module-twitter-feed-container";
    tweetContainer.setAttribute('id', 'tweetcontainer');
    document.getElementById('socialFeed').appendChild(tweetContainer);

    var tweetWrap = document.createElement('div');
    tweetWrap.className = "right-rail-module-social-media-feed-group-wrap";
    tweetWrap.setAttribute('id', 'tweetwrap');    
    document.getElementById('tweetcontainer').appendChild(tweetWrap);

    var tweetGroup = document.createElement('div');
    tweetGroup.className = "right-rail-module-social-media-feed-group";
    tweetGroup.setAttribute('id', 'tweetgroup');    
    document.getElementById('tweetwrap').appendChild(tweetGroup);


    var consumerKey = "XXXXXXXXXXXXXXX";
    var consumerSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
    // Twitter Query Params
    var searchType = "statuses_userTimeline";
    var params = { screen_name : userName, include_rts : true, exclude_replies : true, count : limit };
    var cb = new Codebird;
    cb.setConsumerKey(consumerKey, consumerSecret); 
    cb.__call(
        "oauth2_token",
        {},
        function (reply, err) {
            var bearer_token;
            if (err) {
                console.log("Error encountered: " + err.error);
            }
            if (reply) {
                bearer_token = reply.access_token;
                cb.setBearerToken(bearer_token);
                cb.__call(
                    searchType,
                    params,
                    function (tweets) {
                        for (var key in tweets) {
                            if (key === "statuses") {
                                tweets = tweets[key];
                            }
                        }
                        for (var i = 0; i < tweets.length; i++) {
                            var data = {};
                            for (var key in tweets[i]){
                                if (key === "user") {
                                    for (var prop in tweets[i][key]) {
                                        if (prop === "screen_name") {
                                            data.screen_name = tweets[i][key][prop];
                                        }
                                    }
                                }
                                if (key === "text") {
                                    data.text = tweets[i][key];
                                }
                                if (key === "created_at") {
                                    data.date = tweets[i][key];
                                }
                                if (key === "id_str") {
                                    data.id = tweets[i][key];
                                }
                                if (key === "entities") {
                                    for (var ent in tweets[i][key]) {
                                        if (ent === "media") {
                                            for (var med in tweets[i][key][ent][0]) {
                                                if (med === "media_url_https") {
                                                    data.image = tweets[i][key][ent][0][med];
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            outputTweet(data);

                        }

                         var placeholder = document.createElement('div');
                            placeholder.setAttribute('id', 'placeholder');
                            var lastArticle = document.getElementById('tweetgroup').lastChild;
                            var parentNode = document.getElementById('tweetgroup');
                            lastArticle.parentNode.insertBefore(placeholder, lastArticle.nextSibling);
                    },
                    true
                );
            }
        }
    );
   callback(iscroll);
}

0 个答案:

没有答案