2013-02-14 32 views
0

我正在調用fetch()方法在我的模型得到一個Backbone.js的 - 內模型系列 - 對象函數(){返回新N(A)}沒有方法「有」

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

錯誤。繼承人的代碼:

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,,我發現它確實從服務器獲得了正確的響應,但由於某種原因,它給出了此錯誤。

任何想法?謝謝。

+0

嘗試使用此\t _(this).bindAll(); this.render = _.bind(this.render,this); this.render(); this.model.bind('change:value',this.render); – vaske 2013-02-14 09:22:55

+0

感謝您的回答。我無法弄清楚你想讓我把這段代碼放在哪裏: _(this).bindAll(); this.render = _.bind(this.render,this); this.render(); 你想讓我把它放在WorkoutView的initialize()中嗎? – user1151659 2013-02-14 09:32:29

+0

啊對不起,是在初始化()WorkoutView;) – vaske 2013-02-14 09:34:27

回答

0

好的,在花費了大量時間在縮小的javascript意大利麪的美麗世界之後,我發現我使用的underscore.js版本沒有函數'has'。將underscore.js從1.2.2更新到1.4.4解決了這個問題。此外,我的backbone.js版本是0.9.1

相關問題