0

在带有 Rails 后端的 Backbone 应用程序的 app.js 文件中,我启动了一个集合,并在 fetch() 方法完成对记录的检索后将其设置为 jobForm 视图上的集合。

var app = {

  init: function () {

 this.collections.job  = new this.Collections.job();
 this.collections.job.fetch().complete(function(){
      app.views.jobForm = new app.Views.jobForm({ collection : app.collections.job });

}

在 jobForm 视图(用户输入作业详细信息的视图)中,我监听表单上的提交,然后使用集合在表单提交时创建新作业。

events : {
            'submit form' : 'addDoc'
        },
        addDoc : function(e) {

            e.preventDefault();
            // app.collections.job.create({
            console.log(this.collection);
            this.collection.create({

                job_title : this.$('.job_title').val(),
                position : this.$('.position').val(), 
                company : this.$('.company').val(),

            }, { error : _.bind(this.error, this) });

但是,我收到一个错误

Uncaught TypeError: Object [object Object] has no method 'create' 

该代码甚至没有尝试发出网络请求,因为它认为它没有 create 方法。您会注意到,我在创建它之前对集合进行了控制台日志,它返回了一个作业集合(这是在我将主干添加到应用程序之前创建的),因此它是一个集合。

child {cid: "c1", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
_changing: false
_pending: false
_previousAttributes: Object
attributes: Object
changed: Object
cid: "c1"
__proto__: Surrogate
constructor: function (){ return parent.apply(this, arguments); }
initialize: function () {
model: function (){ return parent.apply(this, arguments); }
url: "/jobs"
__proto__: Object

In the console, I instantiated a new collection, called fetch, retrieved some results, and tried some other collection methods on it, such as collection.at(0), and was told that the object doesn't have the method 'at'.

Strangely, this code (the form linked to a collection) is code that I copied from another working Backbone app of mine.

So, if I'm able to call fetch(); on this object after instantiating it, but not call other Backbone Collection methods (such as 'at' and 'create') what is this object that I'm trying to call create on, and what can I do to fix the problem...

4

1 回答 1

0

Problem was that I was extending a model

app.Collections.job = Backbone.Model.extend({

rather than a collection

app.Collections.job = Backbone.Collection.extend({
于 2013-03-31T04:43:28.260 回答