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
,这对我来说听起来很奇怪简单,很有用。谢谢。请不要按原样使用此答案……这被认为是一种不好的做法,它已被弃用并从下划线中删除。