Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Backbone.js:uncaughttypeerror:Object#<;对象>;没有方法';获取';_Javascript_Jquery_Backbone.js - Fatal编程技术网

Javascript Backbone.js:uncaughttypeerror:Object#<;对象>;没有方法';获取';

Javascript Backbone.js:uncaughttypeerror:Object#<;对象>;没有方法';获取';,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,嗨,我有一个集合,它使用fetch()从API进行初始提取。在用户的交互中,会触发第二次提取,但我没有使用原始的fetch(),而是使用了我自己定义的fetchNew(): 收藏 ListingCollection = Backbone.Collection.extend({ model: Listing, url: '/api/search_by_bounds', fetchNew: function(options) { options = opti

嗨,我有一个集合,它使用
fetch()
从API进行初始提取。在用户的交互中,会触发第二次提取,但我没有使用原始的
fetch()
,而是使用了我自己定义的
fetchNew()

收藏

ListingCollection = Backbone.Collection.extend({
    model: Listing,
    url: '/api/search_by_bounds',

    fetchNew: function(options) {
        options = options || {};
        var collection = this,
            success = options.success;
        options.success = function(resp, status, xhr) {
            _(collection.parse(resp, xhr)).each(function(item) {
                // added this conditional block
                if (!collection.get(item.id)) {
                    // Update collection
                    collection.add(item, {silent:true});
                    // Render View
                    new ListingMarkerView({ model:item }).render();
                }
            });
            if (!options.silent) {
                collection.trigger('reset', collection, options);
            }
            if (success) success(collection, resp);
        };
        return (this.sync || Backbone.sync).call(this, 'read', this, options);
    }
});
这将仅向集合中添加新模型,并仅渲染这些新模型的视图。(如果删除并重新渲染所有视图,将导致闪烁)

查看

ListingMarkerView = Backbone.View.extend({

    render: function() {
        var marker = L.marker([this.model.get('lat'), this.model.get('lng')]);
        markers.addLayer(marker);
    },

    close: function() {
        this.unbind;
    }

});
错误

但是,我得到一个错误:

Uncaught TypeError: Object #<Object> has no method 'get' 
调试

如果我将
console.log(项)
放在呈现
ListingMarkerView

var marker = L.marker([this.model.get('lat'), this.model.get('lng')]);
console.log(item)
new ListingMarkerView({ model:item }).render();
我确实看到一个有效的
项目

Object
    id: "2599084"
    lat: "42.276852"
    lng: "-71.165421"
    price: "2850"
    __proto__: Object
所以

问题

ListingCollection = Backbone.Collection.extend({
    model: Listing,
    url: '/api/search_by_bounds',

    fetchNew: function(options) {
        options = options || {};
        var collection = this,
            success = options.success;
        options.success = function(resp, status, xhr) {
            _(collection.parse(resp, xhr)).each(function(item) {
                // added this conditional block
                if (!collection.get(item.id)) {
                    // Update collection
                    collection.add(item, {silent:true});
                    // Render View
                    new ListingMarkerView({ model:item }).render();
                }
            });
            if (!options.silent) {
                collection.trigger('reset', collection, options);
            }
            if (success) success(collection, resp);
        };
        return (this.sync || Backbone.sync).call(this, 'read', this, options);
    }
});

有什么问题吗?如何解决这个问题?谢谢大家!

问题是渲染没有正确定义
。 在视图类中添加
initialize
方法,如下所示:

initialize: function() {
   _.bindAll(this); //Make all methods in this class have `this` bound to this class
}

我宁愿使用常规的
.reset
,为什么要这样做?你能发布一个到工作站点的链接以便我可以检查吗?Will
.reset
会删除初始
fetch()
中的项目,但不会删除后续调用中的项目吗?我只想将新模型附加到集合中,而不删除旧模型。如果要添加新模型而不删除旧模型,请使用
fetch({add:true})
。但是你应该仍然理解我对你的问题的回答,因为它肯定会在其他地方出现。是的
fetch{add:true})
具有我想要的效果,谢谢!但是,在再次使用
..bindAll
之后,我仍然无法渲染视图:您可以发布一个指向工作站点的链接以便我可以检查吗?我添加了
..bindAll(此“渲染”,“关闭”)但是得到了相同的错误。这个模型等于什么?太好了,现在可以工作了!使用
add:true
并将渲染更改为在
add
上触发,而不是
reset
,这对我来说听起来很奇怪简单,很有用。谢谢。请不要按原样使用此答案……这被认为是一种不好的做法,它已被弃用并从下划线中删除。