gpt4 book ai didi

javascript - Backbone.Collection.where 错误 Uncaught TypeError : i. 匹配不是函数

转载 作者:行者123 更新时间:2023-12-02 15:31:52 25 4
gpt4 key购买 nike

我刚刚开始使用backbone.js,但遇到了一些问题。代码示例:

var Product = Backbone.Model.extend({
defaults: {
id: 0,
category: 0,
material: '',
capacity: 0,
sharpe: 0
},
initialize: function(){
console.log( this.get('id') );
},
Show: function() {
$(".products__item_" + this.get('id')).show('fast');
}
});

var Products = Backbone.Collection.extend({
model: Product
});

var products = new Products();

$( ".products__item" ).each(function( index ) {
var product = new Product({
id: $(this).data('id'),
category: $(this).data('category'),
material: $(this).data('material'),
capacity: $(this).data('capacity'),
sharpe: $(this).data('sharpe')
});
products.add(product);
});

var temp = products.where({ id: 3272 })

因此,当我尝试在 Backbone.Collection 上运行 where 时,我收到 Uncaught TypeError: i.matches is not a function 错误。知道我做错了什么吗?

最佳答案

对我来说看起来不错..我猜这是因为操作顺序,您在加载数据之前运行where()

http://jsfiddle.net/8ru16k5v/

var products = new Backbone.Collection(); 

products.add({
id: 3271,
category: 10,
material: 'wood',
capacity: 10,
sharpe: 10
});
products.add({
id: 3272,
category: 0,
material: 'steal',
capacity: 10,
sharpe: 40
})

var temp = products.where({ id: 3272 })[0].toJSON()
console.log(temp)
alert(JSON.stringify(temp))
PS。请不要在模型中执行 Show()。创建一个 Backbone.View,将您的集合附加到其中。并使用 事件,如 collection.on('sync', doSomethingImportant)

关于javascript - Backbone.Collection.where 错误 Uncaught TypeError : i. 匹配不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33247667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com