0

我得到一个

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,我得到了错误。通过插入警报和console.logs锻炼parse()模型,我发现它确实从服务器获得了正确的响应,但由于某种原因,它给出了这个错误。

有任何想法吗?谢谢。

4

1 回答 1

0

好的,在缩小 javascript spaghetti 的美丽世界中花了很多时间后,我发现我使用的 underscore.js 版本没有函数“has”。将 underscore.js 从 1.2.2 更新到 1.4.4 解决了这个问题。另外,我的backbone.js 版本是0.9.1

于 2013-02-14T11:37:19.043 回答