2012年“未捕获的TypeError:无法在Safari和Chrome上调用方法'getPropertyValue'为null”

时间:2012-10-19 14:40:04

标签: javascript jquery backbone.js requirejs disqus

在我的公司,我们正在努力使用神奇的Disqus小部件提供良好的评论体验。

我们的网站完全基于AJAX,最多使用requirejs,jquery和backbone。

我们已经在我们需要的页面上添加了Disqus小部件。在页面更改期间,我们当然会破坏Disqus小部件所在的div,并再次实例化所有内容。

每个浏览器上的旧小部件都可以。问题发生时激活了2012年的这个小部件,它没有出现错误"未捕获的类型错误:无法调用方法' postMessage'在Safari和Chrome上的null" Firefox效果很好。

以下是涉及的代码:

define([
  'underscore',
  'backbone',
  'models/config',
  'models/book'
], function(_, Backbone, config) {

    App.Models.Disqus = Backbone.Model.extend({
        bookObj: null,

        initialize: function(options){
            console.log("discus.initialize()");
            var _this=this;

            _this.bookObj= new App.Models.Book({id: options.id});
            _this.bookObj.fetch({
                dataType: 'jsonp',
                success: function(model,response){
                    (typeof DISQUS == 'undefined')?_this.initDisqus():_this.resetDisqus();              }
            });

        },

        initDisqus: function(){
            console.log("discus.init()");
            disqus_shortname=config.get('DISQUS_ID');
            disqus_title = this.bookObj.get('content').title; 
            disqus_config = function (){
                // this.page.identifier = _this.id;
                // this.page.url = document.URL;
                this.language = config.get('LANG');
            };
            require(['http://'+disqus_shortname+'.disqus.com/embed.js'], function(){});
        },

        resetDisqus: function(){
            console.log("discus.reset()");
            var _this=this;
            DISQUS.reset({
                reload: true,
                config: function(){
                    this.page.identifier = _this.id;
                    this.page.url = document.URL;
                    this.page.title = _this.bookObj.get('content').title;
                    this.language = config.get('LANG');
                }
            });
        }

    });

    return App.Models.Disqus;
});

如果您需要更多信息,请随时提出。

提前致谢, 乔治

好的伙计们,以这种方式解决了:

define([
  'underscore',
  'backbone',
  'models/config',
  'models/book'
], function(_, Backbone, config) {

    App.Models.Disqus = Backbone.Model.extend({
        bookObj: null,

        initialize: function(options){
            console.log("discus.initialize()");
            var _this=this;
            _this.bookObj= new App.Models.Book({id: options.id});
            _this.bookObj.fetch({
                dataType: 'jsonp',
                success: function(model,response){
                    //(typeof DISQUS == 'undefined')?_this.initDisqus():_this.resetDisqus();

                    delete DISQUS;

                    disqus_shortname = config.get('DISQUS_ID');
                    disqus_identifier = _this.id;
                    disqus_url = document.URL;         
                    disqus_title = _this.bookObj.get('content').title;

                    (function() {
                        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
                    })();

                }
            });
        },
    });

    return App.Models.Disqus;
});

2 个答案:

答案 0 :(得分:1)

正如DISQUS.reset documentation所述,您需要重置Disqus线程的标识符和URL。这是通过文档中提供的两个变量完成的:this.page.identifierthis.page.url。现在你正在重置页面标题和语言。请尝试重置标识符和网址。

答案 1 :(得分:0)

我也有这个问题,但上面的解决方案(删除DISQUS并重新加载页面上的脚本)在严格模式下不起作用。尝试删除DISQUS会导致错误“在严格模式下删除不合格的标识符。”

我解决了这个问题,确保我永远不会删除Disqus附加到自己的容器,这是我在通过ajax新加载的帖子交换帖子时所做的。奇怪的是它没有这个修复它在Firefox中工作,但它现在无处不在。