2013-02-14 1 views
0

내 모델에 fetch() 메소드를 호출상의Backbone.js - 모델 내 컬렉션 - (가) 개체 기능 {새 N을 반환 (A)}있는 방법은 '없다'있다

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

오류를 얻고있다 . Heres the code :

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.logsparse()에 삽입함으로써, 나는 그것이 서버로부터 정확한 응답을 얻는다는 것을 알았지 만, 어떤 이유에서이 에러를 내고있다.

아이디어가 있으십니까? 감사.

+0

시도해보십시오. \t _ (this) .bindAll(); this.render = _.bind (this.render, this); this.render(); this.model.bind ('change : value', this.render); – vaske

+0

답변 해 주셔서 감사합니다. 나는이 코드 조각을 넣으려고 나를 어디로 끌고 가는지 알 수 없다. _ (this) .bindAll(); this.render = _.bind (this.render, this); this.render(); WorkoutView의 initialize()에 넣기를 원하십니까? – user1151659

+0

아, 죄송합니다. 초기화() WorkoutView;) – vaske

답변

0

좋아, 축소 된 자바 스크립트 스파게티의 아름다운 세계에서 많은 시간을 보낸 후, 나는 사용하지 않은 언더 코어 .js 버전에 기능이 있음을 알았습니다. underscore.js를 1.2.2에서 1.4.4로 업데이트하면 문제가 해결되었습니다. 또한, 내 백본 .js 버전은 0.9.1입니다.