Backbone.js-模型中的集合-对象函数(a){return new n(a)}没有方法'有'

Backbone.js - Collection within Model - Object function (a){return new n(a)} has no method 'has'

本文关键字:有方法 模型 js- new 集合 函数 对象 Backbone return      更新时间:2023-09-26

我收到

Object function (a){return new n(a)} has no method 'has'

在我的模型上调用fetch()方法时出错。这是代码:

var Exercise = Backbone.Model.extend({
    defaults: {
        idAttribute: 'e_id',
        e_id: "-1",
        exerciseName: "Exercise",
        exerciseDescription: "Address",
        exerciseURL: "vimeo.com",
        reps: "0",
        sequence: "0"
    },
    initialize: function() {
        alert("Exercise!");
    }
});
var ExerciseList = Backbone.Collection.extend({
    url: "/getWorkoutList.php",
    model: Exercise,
    initialize: function() { }
});
var Workout = Backbone.Model.extend({
    urlRoot: "/getWorkoutList.php",
    url: function() {
        return this.urlRoot + "?workoutID=" + this.get('workoutId');
    },
    defaults: {
        idAttribute: 'workoutId',
        workoutId: "-1",
        workoutName: "WorkoutName",
        workoutDescription: "WorkoutDescription",
        exercises: new ExerciseList()
    }, 
    initialize: function() {
        _.bindAll(this); 
        directory.renderWorkout(this);
    },
    parse: function(response) {
        return response;
    }
});
var WorkoutList = Backbone.Collection.extend({
    url: "/getWorkoutList.php",
    model: Workout,
    initialize: function() {
        _.bindAll(this); 
    },
    parse: function(response) {
        return response;
    }
});
var WorkoutView = Backbone.View.extend({
    tagName: "div",
    className: "workout-container",
    template: $("#tmp-workout").html(),
    initialize: function() {
        _.bindAll(this); 
        this.model.bind('change', this.render, this);
    },
    render: function(){
        console.log("WorkoutView");
        var tmpl = _.template(this.template);
        this.$el.html(tmpl(this.model.toJSON()));
        return this;
    },
    //add ui events
    events: {
        "click #workout-details": "getWorkoutDetails"
    },
    getWorkoutDetails: function (e) {
        e.preventDefault();
        this.model.fetch();
    }
});
var ExerciseView = Backbone.View.extend({
    tagName: "exercise",
    className: "exercise-container",
    template: $("#tmp-exercise").html(),
    initialize: function() {
        _.bindAll(this); 
        alert("ExerciseView");
    },
    render: function(){
        console.log("render exercise view");
        var tmpl = _.template(this.template);
        this.$el.html(tmpl(this.model.toJSON()));
        return this;
    }
});
var WorkoutListingView = Backbone.View.extend({
    el: $("#workouts"),
    initialize: function() {
        var collection = new WorkoutList();
        collection.fetch();
    },
    render: function() {
        var that = this;
        _.each(this.collection.models, function(item){
            that.renderWorkout(item);
        });
    },
    renderWorkout: function(item) {
        var workoutView = new WorkoutView({
            model:item
        });
        this.$el.append(workoutView.render().el);
        var that = this;
        _.each(workoutView.model.get('exercises').models, function(exercise) {
            that.renderExercise(exercise);
        });
    },
    renderExercise: function(item) {
        var exerciseView = new ExerciseView({
            model:item
        });
        this.$el.append(exerciseView.render().el);
    }
});

当我第一次检索锻炼系列时,一切都很好。但是,当我调用getWorkoutDetails时,我会得到错误。通过在锻炼模型的parse()中插入警报和console.logs,我发现它确实从服务器得到了正确的响应,但由于某种原因,它给出了这个错误。

有什么想法吗?谢谢

好吧,在花了很多时间在缩小javascript意大利面条的美丽世界中之后,我发现我使用的undercore.js版本中没有"has"函数。将underscore.js从1.2.2更新到1.4.4解决了这个问题。此外,我的backbone.js版本是0.9.1