javascript Backbone collection.each() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16627116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:24:51  来源:igfitidea点击:

Backbone collection.each() is not working

javascriptbackbone.js

提问by besim dauti

I am receiving the following message when I try to use Backbone's collection.each() method:

当我尝试使用 Backbone 的 collection.each() 方法时,我收到以下消息:

TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'each'.

TypeError: Object function (){ return parent.apply(this, arguments); } 没有方法 'each'

I'm learning Backbone from some Jeffrey Way tutorials; I entered identical code to his, but for some reason it doesn't work.

我正在从一些 Jeffrey Way 教程中学习 Backbone;我输入了与他相同的代码,但由于某种原因它不起作用。

var Person = Backbone.Model.extend({
    defaults: {
        name: 'besim dauti',
        age: 15,
        occupation: 'web developer'
    }
});

var PeopleView = Backbone.View.extend({
    tagName: 'ul',

    render: function(){
        this.collection.each(function(person) {
            var personView = new PersonView({ model: person });
            this.$el.append(personView.render().el)
        }, this)
        return this;
    }
});

var PersonView = Backbone.View.extend({
    tagName: 'li',

    template: _.template( $('#personTemplate').html() ),

    render: function(){
        this.$el.html(this.template(this.model.toJSON()) );
        return this;
    }
});

var PeopleCollection = Backbone.Collection.extend({
    model: Person
});

var peopleCollection = new PeopleCollection([
    {
        name: 'besim',
        age: 25,
        occupation: 'web developer'
    },
    {
        name: 'gaurav',
        age: 25,
        occupation: 'web designer'
    },
    {
        name: 'jeffry',
        age: 27
    }
])

var peopleView = new PeopleView ({collection: PeopleCollection});
$(document.body).append(peopleView.render().el);

Why is there this problem with the .each()function? I typed the code identically and, for Jeffrey, it worked as expected, but for me it displayed this error. I appreciate your help.

为什么.each()函数会出现这个问题?我输入了相同的代码,对于杰弗里,它按预期工作,但对我来说它显示了这个错误。我感谢您的帮助。

回答by George Reith

You need to initialise the PeopleCollection.

您需要初始化PeopleCollection.

See: var peopleView = new PeopleView ({collection: PeopleCollection});

看: var peopleView = new PeopleView ({collection: PeopleCollection});

You are passing in the raw constructor and not an instance of it.

您传入的是原始构造函数而不是它的实例。

You should do this:

你应该做这个:

var peopleCollection = new PeopleCollection(); // Create an instance of PeopleCollection
var peopleView = new PeopleView ({collection: peopleCollection}); // Pass it to peopleView

If you are familiar with OOP (Object orientated programming) think of PeopleCollectionas a class and var peopleCollection = new PeopleCollection()as an instance of that class. You must work with instances.

如果您熟悉 OOP(面向对象的编程),请将其PeopleCollection视为一个类和该类的var peopleCollection = new PeopleCollection()一个实例。您必须使用实例。

You will also encounter an error inside your .eachbecause you capitalised person. Change to:

您还会在您的内部遇到错误,.each因为您将person. 改成:

this.collection.each(function(person) {
   var personView = new PersonView({ model: person });
   this.$el.append(personView.render().el)
}, this)

回答by Scott Puleo

Your code works fine in jsfiddle.

您的代码在 jsfiddle 中运行良好。

http://jsfiddle.net/puleos/kbLES/

http://jsfiddle.net/puleos/kbLES/

var peopleView = new PeopleView ({collection: peopleCollection});
$(document.body).append(peopleView.render().el);

It looks like you may have a dependency issue. Can you post how you are including your scripts on the page?

看起来您可能有依赖性问题。你能发布你如何在页面上包含你的脚本吗?